Skip to content

Null Check when using ModeledResponse with RPC to return a RECORD in SQL #111

@kaushalkumar86

Description

@kaushalkumar86

Null check when using ModeledResponse with RPC calls on SQL.

I am using RPC calls to do all my data manipulations on the server and not on client side.
The SQL query returns row/s as RECORD if successful and null if failed or no result.

The issue arises when its null, the result is in empty json format, ex- {"user" : null, "id" : null, "points" : null}
and when passed via Json Deserialization, it throws invalidcastexception, since the null value cannot be changed to integer.

What I did is created a custom ModeledResponse class and added a null check, as follows

switch (token)
        {
            case JArray:
                {
                    //Null Return Check
                    List<Dictionary<string, object>> data = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(Content!);
                    bool isNull = true;
                    foreach (Dictionary<string,object> item in data)
                    {
                        if (!isNull)
                            break;
                        foreach (KeyValuePair<string, object> pair in item)
                        {
                            if (pair.Value != null)
                            {
                                isNull = false;
                                break;
                            }
                        }
                    }
                    if (isNull)
                        break;
                    //End of Null value check

                    List<T> deserialized = deserialized = JsonConvert.DeserializeObject<List<T>>(Content!, serializerSettings);
                    if (deserialized != null)
                        Models = deserialized;
                    foreach (T model in Models)
                    {
                        model.BaseUrl = baseResponse.ResponseMessage!.RequestMessage.RequestUri.GetInstanceUrl().Replace(model.TableName, "").TrimEnd('/');
                        model.RequestClientOptions = ClientOptions;
                    }

                    break;
                }
            // A single model has been returned
            case JObject:
                {
                    Models.Clear();

                    //Null Return Check
                    Dictionary<string, object> data = JsonConvert.DeserializeObject<Dictionary<string, object>>(Content!);
                    bool isNull = true;
                    foreach (KeyValuePair<string, object> item in data)
                    {
                        if (item.Value != null)
                        {
                            isNull = false;
                            break;
                        }
                    }
                    if (isNull)
                        break;
                    //End of Null value check

                    T obj = JsonConvert.DeserializeObject<T>(Content!, serializerSettings);
                    if (obj != null)
                    {
                        obj.BaseUrl = baseResponse.ResponseMessage!.RequestMessage.RequestUri.GetInstanceUrl().Replace(obj.TableName, "").TrimEnd('/');
                        obj.RequestClientOptions = ClientOptions;

                        Models.Add(obj);
                    }
                    break;
                }
        }

If possible, please add the null check in the class.
Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions