Serialize List<KeyValuePair<string, string>> as JSON - c#

I'm very new with JSON, please help!
I am trying to serialise a List<KeyValuePair<string, string>> as JSON
Currently:
[{"Key":"MyKey 1","Value":"MyValue 1"},{"Key":"MyKey 2","Value":"MyValue 2"}]
Expected:
[{"MyKey 1":"MyValue 1"},{"MyKey 2":"MyValue 2"}]
I referred to some examples from this and this.
This is my KeyValuePairJsonConverter : JsonConverter
public class KeyValuePairJsonConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
List<KeyValuePair<object, object>> list = value as List<KeyValuePair<object, object>>;
writer.WriteStartArray();
foreach (var item in list)
{
writer.WriteStartObject();
writer.WritePropertyName(item.Key.ToString());
writer.WriteValue(item.Value.ToString());
writer.WriteEndObject();
}
writer.WriteEndArray();
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(List<KeyValuePair<object, object>>);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jsonObject = JObject.Load(reader);
var target = Create(objectType, jsonObject);
serializer.Populate(jsonObject.CreateReader(), target);
return target;
}
private object Create(Type objectType, JObject jsonObject)
{
if (FieldExists("Key", jsonObject))
{
return jsonObject["Key"].ToString();
}
if (FieldExists("Value", jsonObject))
{
return jsonObject["Value"].ToString();
}
return null;
}
private bool FieldExists(string fieldName, JObject jsonObject)
{
return jsonObject[fieldName] != null;
}
}
I am calling it from a WebService method like this
List<KeyValuePair<string, string>> valuesList = new List<KeyValuePair<string, string>>();
Dictionary<string, string> valuesDict = SomeDictionaryMethod();
foreach(KeyValuePair<string, string> keyValue in valuesDict)
{
valuesList.Add(keyValue);
}
JsonSerializerSettings jsonSettings = new JsonSerializerSettings { Converters = new [] {new KeyValuePairJsonConverter()} };
string valuesJson = JsonConvert.SerializeObject(valuesList, jsonSettings);

You can use Newtonsoft and dictionary:
var dict = new Dictionary<int, string>();
dict.Add(1, "one");
dict.Add(2, "two");
var output = Newtonsoft.Json.JsonConvert.SerializeObject(dict);
The output is :
{"1":"one","2":"two"}
Edit
Thanks to #Sergey Berezovskiy for the information.
You are currently using Newtonsoft, so just change your List<KeyValuePair<object, object>> to Dictionary<object,object> and use the serialize and deserialize method from the package.

So I didn't want to use anything but native c# to solve a similar issue and for reference this was using .net 4, jquery 3.2.1 and backbone 1.2.0.
My issues was that the List<KeyValuePair<...>> would process out of the controller into a backbone model but when I saved that model the controller could not bind List.
public class SomeModel {
List<KeyValuePair<int, String>> SomeList { get; set; }
}
[HttpGet]
SomeControllerMethod() {
SomeModel someModel = new SomeModel();
someModel.SomeList = GetListSortedAlphabetically();
return this.Json(someModel, JsonBehavior.AllowGet);
}
network capture:
"SomeList":[{"Key":13,"Value":"aaab"},{"Key":248,"Value":"aaac"}]
But even though this set SomeList properly in the backing model.js trying to save the model without any changes to it would cause the binding SomeModel object to have the same length as the parameters in the request body but all the keys and values were null:
[HttpPut]
SomeControllerMethod([FromBody] SomeModel){
SomeModel.SomeList; // Count = 2, all keys and values null.
}
The only things I could find is that KeyValuePair is a structure and not something that can be instantiated in this manner. What I ended up doing is the following:
Add a Model wrapper somewhere that contains key, value fields:
public class KeyValuePairWrapper {
public int Key { get; set; }
public String Value { get; set; }
//default constructor will be required for binding, the Web.MVC binder will invoke this and set the Key and Value accordingly.
public KeyValuePairWrapper() { }
//a convenience method which allows you to set the values while sorting
public KeyValuePairWrapper(int key, String value)
{
Key = key;
Value = value;
}
}
Set up your binding class model to accept your custom wrapper object.
public class SomeModel
{
public List<KeyValuePairWrapper> KeyValuePairList{ get; set };
}
Get some json data out of a controller
[HttpGet]
SomeControllerMethod() {
SomeModel someModel = new SomeModel();
someModel.KeyValuePairList = GetListSortedAlphabetically();
return this.Json(someModel, JsonBehavior.AllowGet);
}
Do something at a later time, maybe model.save(null, ...) is invoked
[HttpPut]
SomeControllerMethod([FromBody] SomeModel){
SomeModel.KeyValuePairList ; // Count = 2, all keys and values are correct.
}

Related

Deserialize json with dynamic objects that starts with pattern

I'm trying to deserialize some json that looks like this
{
"id":"2021",
"descriptions_bg":[
"30231300",
"30233160",
"32420000",
"30214000"
],
"descriptions_cs":[
"30231300",
"30233160",
"32420000",
"30214000"
],
"descriptions_da":[
"30231300",
"30233160",
"32420000",
"30214000"
],
"official_title_bg":"П",
"official_title_cs":"P",
"official_title_da":"P",
"auth_town_bg":"AuthTown",
"auth_town_cs":"AuthTown",
"auth_town_da":"AuthTown"
}
The problem here being, that there can come an infinite number of items in both descriptions_*, official_title_* and auth_town_* with different endings.
I've tried to make classes and members like public string official_title_* { get; set; }in C# to deserialize to with Newtonsoft Json, but that (of course) doesn't work.
Anyway to fix this?
The best option for you is to deserialize to Dictionary<string, JToken> and based on Key and Object Type you can write business logic. With the combination of Linq you can filter the Key
var json = File.ReadAllText("json1.json");
var dict = JsonConvert.DeserializeObject<Dictionary<string, JToken>>(json);
foreach(var item in dict.Where(x=>x.Key.StartsWith("descriptions_")))
{
Console.WriteLine($"{item.Key} is type {item.Value.GetType()}");
}
foreach (var item in dict.Where(x => x.Key.StartsWith("auth_town_")))
{
Console.WriteLine($"{item.Key} is type {item.Value.GetType()}");
}
An alternative method, using a class object that defines each group of properties that have a common prefix as a Dictionary<string, TValue>.
The JSON is deserialized using a custom JsonConverter that maps each group of properties in the JSON to a Property of the class, using a Dictionary<string, string>:
Dictionary<string, string> map = new Dictionary<string, string>() {
["Descriptions"] = "descriptions_",
["OfficialTitles"] = "official_title_",
["AuthTowns"] = "auth_town_"
};
All properties in the JSON that map to a Property in the class are added to the corresponding Dictionary and the values converted to the Type defined by the class property.
Note: you need to adapt the mapper and class Properties to the JSON. You could make the procedure more generic using some logic to determine when Property Name in the JSON belongs to the same group and generate a new Dictionary<string, TValue> to add to the class by reflection.
Or simply add more Properties to the class off-line, if you find other groups.
Call it as:
(possibly giving the class a less silly name :)
var result = new SimpleSequences().Deserialize(json);
Helper class object:
private class SimpleSequences
{
public SimpleSequences() {
Descriptions = new Dictionary<string, long[]>();
OfficialTitles = new Dictionary<string, string>();
AuthTowns = new Dictionary<string, string>();
}
public List<SimpleSequences> Deserialize(string json)
{
var options = new JsonSerializerSettings() {
Converters = new[] { new SimpleSequencesConverter() }
};
return JsonConvert.DeserializeObject<List<SimpleSequences>>(json, options);
}
public int Id { get; set; }
public Dictionary<string, long[]> Descriptions { get; set; }
public Dictionary<string, string> OfficialTitles { get; set; }
public Dictionary<string, string> AuthTowns { get; set; }
}
Custom JsonConverter:
This converter handles a single object or array of objects (as shown in your question), but always return a List<SimpleSequences> (which may contain a single object). Modify as required.
public class SimpleSequencesConverter : JsonConverter
{
public override bool CanConvert(Type objectType) => true;
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType != JsonToken.StartArray && reader.TokenType != JsonToken.StartObject) {
throw new Exception($"Unexpected Type. Expecting Array or Object, got {reader.TokenType}");
}
var elementsList = new List<SimpleSequences>();
if (reader.TokenType == JsonToken.StartObject) {
elementsList.Add(GetPropertyValues(JObject.Load(reader)));
}
else {
while (reader.Read() && reader.TokenType != JsonToken.EndArray) {
elementsList.Add(GetPropertyValues(JObject.Load(reader)));
}
}
return elementsList;
}
private SimpleSequences GetPropertyValues(JToken token)
{
var element = new SimpleSequences();
element.Id = token["id"].ToObject<int>();
var descriptions = token.Children().Where(t => t.Path.StartsWith(map["Descriptions"]));
foreach (JProperty p in descriptions) {
element.Descriptions.Add(p.Path, p.Value.ToObject<long[]>());
}
var titles = token.Children().Where(t => t.Path.StartsWith(map["OfficialTitles"]));
foreach (JProperty p in titles) {
element.OfficialTitles.Add(p.Path, p.Value.ToString());
}
var authTowns = token.OfType<JToken>().Where(t => t.Path.StartsWith(map["AuthTowns"]));
foreach (JProperty p in authTowns) {
element.AuthTowns.Add(p.Path, p.Value.ToString());
}
return element;
}
public override bool CanWrite => false;
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
=> throw new NotImplementedException();
Dictionary<string, string> map = new Dictionary<string, string>() {
["Descriptions"] = "descriptions_",
["OfficialTitles"] = "official_title_",
["AuthTowns"] = "auth_town_"
};
}
Yet another solution could be to use several LINQ2JSON queries.
For instance, if you want to get all description_xyz collections in a single list
then you can do that like this:
var semiParsedJson = JObject.Parse(json);
var descriptionProperties = semiParsedJson.Properties()
.Where(prop => prop.Name.StartsWith("descriptions_"));
var collectionOfDescriptions =
from desc in descriptionProperties.Values()
select ((JArray)desc).ToObject<IEnumerable<string>>();
var flattenedDescriptions = collectionOfDescriptions.
SelectMany(desc => desc)
.ToList();
We can semi parse the json by using JObject.Parse
We can perform some filtering based on the properties' Name field
We can then get all values form each description_xyz collections as IEnumerable<string>
We will have at this point a collection of collections IEnumerable<IEnumerable<string>>, which we can flatten with SelectMany
At very end we can materialize the query with ToList

Converting boolean to int in json string

I there,
I'm working on a c# application
I Have a situation where i get an object from a web service, say
MyObject{
public bool MyProp
}
And I can't modify that object,
but i need to serialize MyObject to a json string but MyProp has to be converted to 1 or 0 instead of true/false.
I'm using JavaScriptSerializer to serialize to Json
Any idea?
tks
If you are willing to switch to json.net, you can use the solution from Convert an int to bool with Json.Net.
If you wish to continue using JavaScriptSerializer, you will need to create a JavaScriptConverter for your MyObject type as follows:
class MyObjectConverter : JavaScriptConverter
{
public override IEnumerable<Type> SupportedTypes
{
get { return new[] { typeof(MyObject) }; }
}
// Custom conversion code below
const string myPropName = "MyProp";
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
object value;
if (dictionary.TryGetValue(myPropName, out value))
{
dictionary[myPropName] = !value.IsNullOrDefault();
}
var myObj = new JavaScriptSerializer().ConvertToType<MyObject>(dictionary);
return myObj;
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
var myObj = (MyObject)obj;
// Generate a default serialization. Is there an easier way to do this?
var defaultSerializer = new JavaScriptSerializer();
var dict = defaultSerializer.Deserialize<Dictionary<string, object>>(defaultSerializer.Serialize(obj));
dict[myPropName] = myObj.MyProp ? 1 : 0;
return dict;
}
}
public static class ObjectExtensions
{
public static bool IsNullOrDefault(this object value)
{
// Adapted from https://stackoverflow.com/questions/6553183/check-to-see-if-a-given-object-reference-or-value-type-is-equal-to-its-default
if (value == null)
return true;
Type type = value.GetType();
if (!type.IsValueType)
return false; // can't be, as would be null
if (Nullable.GetUnderlyingType(type) != null)
return false; // ditto, Nullable<T>
object defaultValue = Activator.CreateInstance(type); // must exist for structs
return value.Equals(defaultValue);
}
}
Then use it like:
var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new JavaScriptConverter[] { new MyObjectConverter() } );
var json = serializer.Serialize(myObject);
Note - even though your MyObject class only has one property, I wrote the converter under the assumption that in real life it could have additional properties that should be serialized and deserialized automatically, for instance:
public class MyObject
{
public bool MyProp { get; set; }
public string SomeOtherProperty { get; set; }
}

How to override JavaScriptSerializer.Deserialize<T>()

I get a json string which have few data uniformity issue.
For example one field in json string returns a list of string while the same field in other json string returns a dictionary(key, value pairs).
My class which holds the parsed json values have property for the field as List.
Because of this data uniformity problem, the json string is not parsed properly.
Following is my code to parse the json string
JavaScriptSerializer serializer = new JavaScriptSerializer();
myClass mc = serializer.Deserialize<myClass>(jsonString);
IS there any way with which i can write custom code to parse the json string and map it to myClass?
You don't give a concrete example of what you are trying to accomplish, which means we need to make up an example ourselves. Consider the following class:
public class myClass
{
public Dictionary<string, string> data { get; set; }
}
And consider the following two JSON strings:
{"data": ["zero", 1, "two"]}
{"data": {"0": "zero", "1":1, "2":"two"}}
It seems like you might like to parse these identically, with the array being converted to a Dictionary<string, string> whose keys are array indices. This can be accomplished with the following JavaScriptConverter:
public class myClassConverter : JavaScriptConverter
{
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
var myClass = new myClass();
object data;
if (dictionary.TryGetValue("data", out data))
{
if (data.IsJsonArray())
{
myClass.data = data.AsJsonArray()
.Select((o, i) => new KeyValuePair<int, object>(i, o))
.ToDictionary(p => p.Key.ToString(NumberFormatInfo.InvariantInfo), p => serializer.ConvertToType<string>(p.Value));
}
else if (data.IsJsonObject())
{
myClass.data = data.AsJsonObject()
.ToDictionary(p => p.Key, p => serializer.ConvertToType<string>(p.Value));
}
}
return myClass;
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
public override IEnumerable<Type> SupportedTypes
{
get { return new [] { typeof(myClass) }; }
}
}
public static class JavaScriptSerializerObjectExtensions
{
public static bool IsJsonArray(this object obj)
{
if (obj is string || obj.IsJsonObject())
return false;
return obj is IEnumerable;
}
public static IEnumerable<object> AsJsonArray(this object obj)
{
return (obj as IEnumerable).Cast<object>();
}
public static bool IsJsonObject(this object obj)
{
return obj is IDictionary<string, object>;
}
public static IDictionary<string, object> AsJsonObject(this object obj)
{
return obj as IDictionary<string, object>;
}
}
The IDictionary<string, object> passed to Deserialize() corresponds to the key/value pairs in the JSON object being converted. For a particular key ("data" in this case) the object value will be an IDictionary<string, object> if the value is, in turn, a JSON object, and an IEnumerable (specifically an ArrayList) if the value is a JSON array. By testing the value against the appropriate type, a conversion can be made.
The converter only does deserialization. Use it like so:
var jsonString1 = #"{""data"": [""zero"", 1, ""two""]}";
var jsonString2 = #"{""data"": {""0"": ""zero"", ""1"":1, ""2"":""two""}}";
var deserializer = new JavaScriptSerializer();
deserializer.RegisterConverters(new JavaScriptConverter[] { new myClassConverter() });
var newJson1 = new JavaScriptSerializer().Serialize(deserializer.Deserialize<myClass>(jsonString1));
var newJson2 = new JavaScriptSerializer().Serialize(deserializer.Deserialize<myClass>(jsonString2));
Console.WriteLine(newJson1); // Prints {"data":{"0":"zero","1":"1","2":"two"}}
Console.WriteLine(newJson2); // Prints {"data":{"0":"zero","1":"1","2":"two"}}
Debug.Assert(newJson1 == newJson2); // No assert

How to handle deserialization of empty string into long in .NET?

The below illustration represents building Object of type TestClass2 using object of type TestClass1 with the help of Serialization/Deserialization.
TestClass1 and TestClass2 have the same structure except one of the members is string in TestClass1 but long in TestClass2.
public class TestClass1
{
public string strlong;
}
public class TestClass2
{
public long strlong;
}
TestClass1 objT1 = new TestClass1();
objT1.strlong = "20134567";
TestClass2 objT2;
JavaScriptSerializer serializer = new JavaScriptSerializer();
string JSON1 = serializer.Serialize(objT1);
objT2 = serializer.Deserialize<TestClass2>(JSON1);
After the operation, objT2 will have the values of objT1 but strlong will now be long as opposed to string.
The problem is, if the strlong value in objT1 is an empty string --> "", the deserialization fails with an exception "" is not a valid value for Int64.
If strlong is non empty string with just numeric characters, the current deserialization works. But I do not know the workaround when something like empty string appears.
For now, lets assume that
strlong will be in the range of long
Will just be a sequence of numeric characters i.e. it will not have . or , or / or any type of other characters
Have access to only objects for serialization and I cannot make modifications to TestClass1 or TestClass2.
If there is a simple way (or not) of Creating objects of one class using objects of another class, please mention that in the comments.
EDIT-Extending the logic
To extend the logic of solution given in the Answer below to Classes containing members of type other classes, I have used the serialization solution given below to the member items as well. In other words, if classes contain members of other classes, is there a better way of handling the deeper levels than the code below?
// **Item1 :**
// These are the subclasses and classes
// whose objects I am trying to serialize
// and deserialize from one type to another
public class SubClass1
{
public string toomuch;
public int number = 30;
}
public class SubClass2
{
public long toomuch;
public int number;
}
public class TestClass1
{
public string strlong;
public SubClass1 item2;
}
public class TestClass2
{
public long strlong;
public SubClass2 item2;
}
// **Item2 :**
// Solution from StackOverflow for serialization of
// empty string
public class TestClass1Converter : JavaScriptConverter
{
public override IEnumerable<Type> SupportedTypes
{
get { return new Type[] { typeof(TestClass1) }; }
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
var data = obj as TestClass1;
var dic = new Dictionary<string, object>();
if (data == null)
{
return dic;
}
long val = 0;
long.TryParse(data.strlong, out val);
dic.Add("strlong", val);
// **Item3 :**
// trying to serialize and deserialize item2 which is of type SubClass1
// which might also have empty string
/*******************/
JavaScriptSerializer subClassSerializer = new JavaScriptSerializer();
subClassSerializer.RegisterConverters(new[] { new SubClass1Converter() });
string JSONstr = subClassSerializer.Serialize(data.item2);
dic.Add("item2", subClassSerializer.Deserialize<SubClass2>(JSONstr));
/*******************/
return dic;
}
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
}
// **Item4 :**
// Serialization for subclass
public class SubClass1Converter : JavaScriptConverter
{
public override IEnumerable<Type> SupportedTypes
{
get { return new Type[] { typeof(SubClass1) }; }
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
var data = obj as SubClass1;
var dic = new Dictionary<string, object>();
if (data == null)
{
return dic;
}
long val = 0;
long.TryParse(data.toomuch, out val);
dic.Add("toomuch", val);
dic.Add("number", data.number);
return dic;
}
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
}
class Program
{
static void Main(string[] args)
{
TestClass1 objT1 = new TestClass1();
objT1.strlong = "";
SubClass1 objSub = new SubClass1();
objSub.toomuch = "";
objT1.item2 = objSub;
TestClass2 objT2;
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { new TestClass1Converter() });
string JSON1 = serializer.Serialize(objT1);
objT2 = serializer.Deserialize<TestClass2>(JSON1);
}
}
You should declare your TestClass2.strlong as nullable.
public class TestClass2
{
public long? strlong;
}
Now you can have null in case when the TestClass1.strlong is empty string or null.
Here is UPDATE in case that you haven't access to modify the classes.
You should add to the serializer the converter via RegisterConverters to customize conversion. Here is the example:
public class TestClass1Converter : JavaScriptConverter
{
public override IEnumerable<Type> SupportedTypes
{
get { return new Type[] { typeof(TestClass1)}; }
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
var data = obj as TestClass1;
var dic = new Dictionary<string, object>();
if(data == null)
{
return dic;
}
long val = 0;
long.TryParse(data.strlong, out val);
dic.Add("strlong", val);
return dic;
}
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
}
This converter will serialize strlong to 0 in case when it is not convertible to long. You can use it in this way:
TestClass1 objT1 = new TestClass1();
objT1.strlong = "444";
TestClass2 objT2;
JavaScriptSerializer serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new [] {new TestClass1Converter()});
string JSON1 = serializer.Serialize(objT1);
objT2 = serializer.Deserialize<TestClass2>(JSON1);

passing an additional parameter to JavascriptSerializer

I'm using JavascriptSerializer with custom JavascriptConverter like this:
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
SomeObjectModel TheObject = obj as SomeObjectModel;
Dictionary<string, object> OutputJson = new Dictionary<string, object>();
OutputJson.Add("SomeKey", SomeObjectModel.SomeProperty);
return OutputJson;
}
Now I want to be able to dynamically change SomeKey at runtime so I thought of creating a dictionary of keys, passing this dictionary to the serializer, and doing something like this:
OutputJson.Add(TheJsonDictionary.SomeKey, SomeObjectModel.SomeProperty);
However, when I add a parameter to the function call like this:
public override IDictionary<string, object> Serialize(Dictionary<string, string> TheJsonDictionary, object obj, JavaScriptSerializer serializer)
I get an error message at compilation. Now I know why I'm getting this error (the abstract method is defined with 2 parameters and I'm passing 3) and I'm wondering how to go around this so that I can pass in a dictionary to encode the keys.
Thanks.
I guess the only workaround would be to subclass the JavaScriptSerializer and reimplement the Serialize method to accept an arbitrary TheJsonDictionary dictionary:
class CustomJavaScriptSerializer : JavaScriptSerializer
{
public Dictionary<string, string> TheJsonDictionary { get; private set; }
public string Serialize(object obj, Dictionary<string, string> TheJsonDictionary = null)
{
this.TheJsonDictionary = TheJsonDictionary;
return base.Serialize(obj);
}
}
Now in your custom converter you can cast the serializer object back to CustomJavaScriptSerializer and get the custom diectionary of keys:
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
var TheJsonDictionary = (serializer as CustomJavaScriptSerializer).TheJsonDictionary;
// rest of method
}
As I understood, this is what you need:
class Program
{
static void Main(string[] args)
{
var dict = new Dictionary<string, string>();
//dict.Add("SomeKey", "SomeProperty1");
dict.Add("SomeKey", "SomeProperty2");
var myConverter = new MyConverter();
var serialized = myConverter.Serialize(dict, new SomeObjectModel() { SomeProperty1 = 1, SomeProperty2 = 2 }, new MySerializer());
}
class MySerializer : JavaScriptSerializer
{ }
class MyConverter : JavaScriptConverter
{
Dictionary<string, string> _theJsonDictionary;
public IDictionary<string, object> Serialize(Dictionary<string, string> TheJsonDictionary, object obj, JavaScriptSerializer serializer)
{
_theJsonDictionary = TheJsonDictionary;
return Serialize(obj, serializer);
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
SomeObjectModel TheObject = obj as SomeObjectModel;
Dictionary<string, object> OutputJson = new Dictionary<string, object>();
foreach (var item in _theJsonDictionary.Keys)
{
OutputJson.Add(item, TheObject.GetType().GetProperty(_theJsonDictionary[item]).GetValue(TheObject));
}
return OutputJson;
}
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
return new object();
}
public override IEnumerable<Type> SupportedTypes
{
get { return new[] { typeof(object) }.AsEnumerable(); }
}
}
class SomeObjectModel
{
public int SomeProperty1 { get; set; }
public int SomeProperty2 { get; set; }
}
}

Categories