You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on May 16, 2022. It is now read-only.
//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;}
}
}
`
The text was updated successfully, but these errors were encountered:
reddyrajanala
changed the title
Issue serializing List<KeyValuePai<string,string>> in camelCase
Issue serializing List<KeyValuePair<string,string>> in camelCase
Jun 25, 2020
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 :
//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()
{
}
`
The text was updated successfully, but these errors were encountered: