neuecc / Utf8Json

Definitely Fastest and Zero Allocation JSON Serializer for C#(NET, .NET Core, Unity, Xamarin).
MIT License
2.35k stars 266 forks source link

Issue serializing List<KeyValuePair<string,string>> in camelCase #227

Open reddyrajanala opened 4 years ago

reddyrajanala commented 4 years ago

//I am unable to serialize List<KVP<string,string>> with kvp names as "key"/"value" //similarly cannot deserialize //See the snippet below ` using System; using Utf8Json; using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using Utf8Json.Resolvers; using Utf8Json.Formatters;

public class Program { public static void Main() {

    CompositeResolver.RegisterAndSetAsDefault(
        new IJsonFormatter[] {new KeyValuePairFormatter<string, string>()},  //this did not help
        new[] {StandardResolver.CamelCase});

    var p = new Person{
     FirstName = "Bob",
        LastName ="Smith",
        Others = new List<KeyValuePair<string, string>>
        {
            new KeyValuePair<string, string>("foo1", "bar1"),
            new KeyValuePair<string, string>("foo2", "bar2"),
            new KeyValuePair<string, string>("foo3", "bar3"),
        }
    };

    var utf8SerializedJson = Utf8Json.JsonSerializer.ToJsonString(p, Utf8Json.Resolvers.StandardResolver.CamelCase);
    Console.WriteLine(utf8SerializedJson);
    //ouptput : note "Key" and "Value" names are not in camelcase
    //{"firstName":"Bob","lastName":"Smith","others":[{"**Key**":"foo1","**Value**":"bar1"},{"Key":"foo2","Value":"bar2"},{"Key":"foo3","Value":"bar3"}]}

    var newtonSoftSerializedJson = Newtonsoft.Json.JsonConvert.SerializeObject(p, new JsonSerializerSettings 
{ 
    ContractResolver = new CamelCasePropertyNamesContractResolver() 
});
    Console.WriteLine(newtonSoftSerializedJson);
    //output : note "key" and "value" are in camelcase
    //{"firstName":"Bob","lastName":"Smith","others":[{"key":"foo1","value":"bar1"},{"key":"foo2","value":"bar2"},{"key":"foo3","value":"bar3"}]}

    //key and value as "Key" and "Value" - desierializes as expected
    var ds1 = @"{""firstName"":""Sri"",""lastName"":""Raj"",""others"":[{""Key"":""foo1"",""Value"":""bar1""},{""Key"":""foo2"",""Value"":""bar2""},{""Key"":""foo3"",""Value"":""bar3""}]}";

    var person1 = Utf8Json.JsonSerializer.Deserialize<Person>(ds1);
    Console.WriteLine(person1.Others.First().Key + "-" + person1.Others.First().Value );

    //key and value as "key" and "value" - fails to deseiralize as expected
    var ds2 = @"{""firstName"":""Sri"",""lastName"":""Raj"",""others"":[{""key"":""foo1"",""value"":""bar1""},{""key"":""foo2"",""value"":""bar2""},{""key"":""foo3"",""value"":""bar3""}]}";

    var person2 = Utf8Json.JsonSerializer.Deserialize<Person>(ds2);

    //expected: "foo1 - bar1" actual: " - "
    Console.WriteLine(person2.Others.First().Key + "-" + person2.Others.First().Value );

}

public class Person
{
    public string FirstName{get;set;}
    public string LastName{get;set;}
    public List<KeyValuePair<string, string>> Others {get;set;}
}

} `

bokmadsen commented 3 years ago

I've looking for the same solution, but it's not easy. It would require a modification of the code because it's hardcoded to instantiate the builtin KeyValueFormatter here :https://github.com/neuecc/Utf8Json/blob/608cf01589cb3feb225a6e51a2324a24466fdaa6/src/Utf8Json/Resolvers/DynamicGenericResolver.cs#L124 I don't think a PR would be enough, as it seems the project is abandoned