Created
August 11, 2011 16:55
-
-
Save Buildstarted/1140171 to your computer and use it in GitHub Desktop.
Simple ExpandoObject-Like dictionary for NetwonSoft's Json.Net parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JDictionary { | |
private List<JDictionary> items; | |
public string Key { get; set; } | |
public object Value { get; set; } | |
public JDictionary() { | |
items = new List<JDictionary>(); | |
} | |
public JDictionary this[int index] { | |
get { return items[index]; } | |
} | |
public JDictionary this[string key] { | |
get { return items.First(f => f.Key == key); } | |
} | |
internal JDictionary Add(string key, object value) { | |
JDictionary dict = new JDictionary() { Key = key, Value = value }; | |
items.Add(dict); | |
return dict; | |
} | |
internal JDictionary Add(JDictionary dictionary) { | |
items.Add(dictionary); | |
return dictionary; | |
} | |
} | |
public class JDictionaryConverter : JsonConverter { | |
public override bool CanConvert(Type objectType) { | |
return objectType == typeof(JDictionary); | |
} | |
private object ReadValue(JsonReader reader) { | |
while (reader.TokenType == JsonToken.Comment) { | |
if (!reader.Read()) | |
throw new Exception("Unexpected end."); | |
} | |
switch (reader.TokenType) { | |
case JsonToken.StartObject: | |
return ReadObject(reader); | |
case JsonToken.StartArray: | |
return ReadList(reader); | |
default: | |
return reader.Value; | |
} | |
} | |
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { | |
return ReadValue(reader); | |
} | |
private object ReadList(JsonReader reader) { | |
JDictionary list = new JDictionary(); | |
while (reader.Read()) { | |
switch (reader.TokenType) { | |
case JsonToken.Comment: | |
break; | |
default: | |
object v = ReadValue(reader); | |
if (v.GetType() == typeof(JDictionary)) { | |
var item = (JDictionary)v; | |
list.Add(item); | |
} else { | |
list.Add(null, v); | |
} | |
break; | |
case JsonToken.EndArray: | |
return list; | |
} | |
} | |
throw new Exception("Unexpected end."); | |
} | |
private object ReadObject(JsonReader reader) { | |
JDictionary dict = new JDictionary(); | |
while (reader.Read()) { | |
switch (reader.TokenType) { | |
case JsonToken.PropertyName: | |
string propertyName = reader.Value.ToString(); | |
if (!reader.Read()) | |
throw new Exception("Unexpected end."); | |
object v = ReadValue(reader); | |
if (v.GetType() == typeof(JDictionary)) { | |
var item = (JDictionary)v; | |
item.Key = propertyName; | |
dict.Add(item); | |
} else { | |
dict.Add(propertyName, v); | |
} | |
break; | |
case JsonToken.Comment: | |
break; | |
case JsonToken.EndObject: | |
return dict; | |
} | |
} | |
throw new Exception("Unexpected end."); | |
} | |
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { } | |
public static JDictionary Parse(string json) { | |
return (JDictionary)JsonConvert.DeserializeObject(json, typeof(JDictionary), new JDictionaryConverter()); | |
} | |
public override bool CanWrite { | |
get { return false; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment