How to serialize or deserialize a json with lowercase keys and uppercase values (newtonsoft)

Asked

Viewed 75 times

-1

I have the following JSON string example: :

{ 
   myproperty : 'Hello' 
}

When I desiccate you or serialize you, I want you to stay that way:

{
    myproperty : 'HELLO'
}

All values in Uppercase and All Keys in Lowercase

Code :

deserialize :

var jsonObj = JsonConvert.DeserializeObject<Rootobject>(jsonString);

serialize :

var strJson = JsonConvert.SerializeObject(dataTable);
  • You will have to create a customization pro serializer. He even treats the naming convention for the properties (the keys, in this case), but I don’t remember having any configuration to manipulate the values.

  • To customize the keys I already found, now I really do not find in doc something to make all values in uppercase.

2 answers

0

After consulting this post: Post!

I set up the code my way, need to improve , but will solve, including pq manipulate data to Insert in Firebird.

Here is the code :

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JObject obj = (JObject)JObject.Load(reader);
            string jsonrpc = obj["jsonrpc"].ToString();
            int id = Convert.ToInt32(obj["id"]);
            JArray array = (JArray)obj["result"]["lista"];
            var listaProps = typeof(Lista).GetProperties().ToList();
            foreach (JObject item in array)
            {
                foreach (var props in listaProps)
                {
                    try
                    {
                        switch (item[props.Name].Type)
                        {
                            case JTokenType.String:
                                item[props.Name] = item[props.Name].ToString().ToUpper();
                                break;
                            case JTokenType.Null:
                                item[props.Name] = "'NULL'";
                                break;
                            default:
                                break;
                        }
                    }
                    catch (Exception)
                    {

                    }

                }
            }
            var pizzaJson = new JsonPizza();
            pizzaJson.id = id;
            pizzaJson.result = new Result();
            pizzaJson.result.lista = array.ToObject<Lista[]>();
            pizzaJson.result.qtd = array.Count();
            pizzaJson.jsonrpc = jsonrpc;
            return pizzaJson;
        }

To Deserializeobject :

var obj = JsonConvert.DeserializeObject<JsonPizza>(json, new PizzaPagedListConverter(typeof(JsonPizza)));

The Class PizzaPagedListConverter :

internal class PizzaPagedListConverter : JsonConverter

To leave all string values in UPPERCASE when Serialize :

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            JToken t = JToken.FromObject(value);

            if (t.Type == JTokenType.String)
            {
                t = t.ToString().ToUpper();
                t.WriteTo(writer);
            }
        }

-1

  • That is not exactly the question of the author of the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.