private JsonArray SerializeDictionary(IDictionary dictionary) { //Prepare JsonArray jsonArray = new JsonArray(); //Check if (dictionary == null) { throw new ArgumentNullException(nameof(dictionary)); } //Loop foreach (object key in dictionary.Keys) { //Serialize key JsonObject keyObject = Serialize(key); //Serialize value JsonObject valueObject = Serialize(dictionary[key]); //Create new collection JsonElementCollection jsonKeyValuePair = new JsonElementCollection(); jsonKeyValuePair.Add(new JsonElement() { Name = "key", Value = keyObject }); jsonKeyValuePair.Add(new JsonElement() { Name = "value", Value = valueObject }); //Add jsonArray.Elements.Add(jsonKeyValuePair); } //Return return(jsonArray); }
private JsonElementCollection SerializeObject(object @object) { //Prepare JsonElementCollection collection = new JsonElementCollection(); JsonSerializeAttribute attribute; //Check if (@object == null) { throw new ArgumentNullException(nameof(@object)); } Type objectType = @object.GetType(); //Loop through properties in object foreach (PropertyInfo propertyInfo in objectType.GetProperties()) { //Prepare JsonElement element = new JsonElement() { Name = propertyInfo.Name }; try { //Get property value object propertyValue = propertyInfo.GetValue(@object); if (propertyValue is Delegate) { element.Value = new JsonNull(); } else if (propertyValue is Type type) { element.Value = new JsonString() { Value = type.AssemblyQualifiedName } } ; else { //Get JsonSerializeAttribute attribute = propertyInfo.GetCustomAttribute <JsonSerializeAttribute>(); if (attribute != null) { //Check name if (!string.IsNullOrEmpty(attribute.Name)) { element.Name = attribute.Name; } } //Set value element.Value = Serialize(propertyValue); } } catch { element.Value = new JsonNull(); } //Add element collection.Add(element); } //Loop through fields in object foreach (FieldInfo fieldInfo in objectType.GetFields()) { //Prepare JsonElement element = new JsonElement() { Name = fieldInfo.Name }; try { //Get field value object fieldValue = fieldInfo.GetValue(@object); //Get JsonSerializeAttribute attribute = fieldInfo.GetCustomAttribute <JsonSerializeAttribute>(); if (attribute != null) { //Check name if (!string.IsNullOrEmpty(attribute.Name)) { element.Name = attribute.Name; } } //Set value element.Value = Serialize(fieldValue); } catch { element.Value = new JsonNull(); } //Add element collection.Add(element); } //Return return(collection); }
/// <summary> /// Converts the specified string to a JSON object. /// </summary> /// <param name="json">The JSON string. This value can be null.</param> /// <returns>A JSON object representing the specified string.</returns> public static JsonObject Parse(string json) { //Check if (string.IsNullOrEmpty(json)) { return(new JsonNull()); } //Create reader using (JsonStringReader reader = new JsonStringReader(json)) { //Read first token if (reader.PeekToken() == -1) { return(new JsonNull()); } string token = reader.ReadToken(); //Check peek char switch (GetTokenType(token)) { case JsonTokenType.Number: return(new JsonNumber() { Value = decimal.Parse(token) }); case JsonTokenType.String: return(new JsonString() { Value = FormatString(token) }); case JsonTokenType.Collection: JsonElementCollection collection = new JsonElementCollection(); using (JsonStringReader collectionReader = new JsonStringReader(token)) { //Read open brace collectionReader.Read(); //Loop while (collectionReader.PeekToken() != '}') { //Read key-value pair string nameToken = collectionReader.ReadString(); collectionReader.ReadToken(); string valueToken = collectionReader.ReadToken(); //Check if (collectionReader.PeekToken() == ',') { collectionReader.ReadToken(); } collection.Add(new JsonElement() { Name = FormatString(nameToken), Value = Parse(valueToken) }); } //Read close brace collectionReader.Read(); } return(collection); case JsonTokenType.Array: JsonObjectCollection array = new JsonObjectCollection(); using (JsonStringReader arrayReader = new JsonStringReader(token)) { //Read open bracket arrayReader.Read(); //Loop while (arrayReader.PeekToken() != ']') { //Read object string elementToken = arrayReader.ReadToken(); //Check if (arrayReader.PeekToken() == ',') { arrayReader.ReadToken(); } array.Add(Parse(elementToken)); } //Read close bracket arrayReader.Read(); } return(array); case JsonTokenType.Literal: if (token == "true" || token == "false") { return new JsonBoolean() { Value = bool.Parse(token) } } ; else if (token == "null") { return(new JsonNull()); } throw new InvalidDataException("Only the 'true', 'false', and 'null' literals are supported."); } } //Return return(null); }