/// <summary> /// Deserialize the json datatable token to the desired object /// </summary> /// <param name="jsonToken">The json datatable token to be deserialized</param> /// <param name="dataType">The type of the desired object</param> /// <returns>The desired object instance</returns> public override Object Deserialize(LazyJsonToken jsonToken, Type dataType) { if (jsonToken == null || jsonToken.Type != LazyJsonType.Object || dataType == null || dataType != typeof(DataTable)) { return(null); } LazyJsonObject jsonObjectDataTable = (LazyJsonObject)jsonToken; DataTable dataTable = new DataTable(); // Rows if (jsonObjectDataTable["Rows"] != null && jsonObjectDataTable["Rows"].Token.Type == LazyJsonType.Array) { LazyJsonArray jsonArrayDataTableRows = (LazyJsonArray)jsonObjectDataTable["Rows"].Token; LazyJsonDeserializerDataRow jsonDeserializerDataRow = new LazyJsonDeserializerDataRow(); foreach (LazyJsonToken jsonTokenDataTableRow in jsonArrayDataTableRows.TokenList) { if (jsonTokenDataTableRow.Type == LazyJsonType.Object) { jsonDeserializerDataRow.Deserialize(jsonTokenDataTableRow, dataTable); } } } return(dataTable); }
/// <summary> /// Serialize an object to a json datatable object token /// </summary> /// <param name="data">The object to be serialized</param> /// <returns>The json datatable object token</returns> public override LazyJsonToken Serialize(Object data) { if (data == null || data is not DataTable) { return(new LazyJsonNull()); } DataTable dataTable = (DataTable)data; LazyJsonObject jsonObjectDataTable = new LazyJsonObject(); // Rows LazyJsonArray jsonArrayDataTableRows = new LazyJsonArray(); jsonObjectDataTable.Add(new LazyJsonProperty("Rows", jsonArrayDataTableRows)); LazyJsonSerializerDataRow jsonSerializerDataRow = new LazyJsonSerializerDataRow(); foreach (DataRow dataRow in dataTable.Rows) { LazyJsonToken jsonTokenDataTableRow = jsonSerializerDataRow.Serialize(dataRow); if (jsonTokenDataTableRow != null && jsonTokenDataTableRow.Type != LazyJsonType.Null) { jsonArrayDataTableRows.Add(jsonTokenDataTableRow); } } return(jsonObjectDataTable); }
/// <summary> /// Serialize an object to a json array token /// </summary> /// <param name="data">The object to be serialized</param> /// <returns>The json array token</returns> public override LazyJsonToken Serialize(Object data) { if (data == null) { return(new LazyJsonNull()); } Type dataType = data.GetType(); if (dataType.IsArray == false) { return(new LazyJsonNull()); } Array dataArray = (data as Array); LazyJsonArray jsonArray = new LazyJsonArray(); Type arrayElementType = dataType.GetElementType(); Type jsonSerializerType = LazyJsonSerializer.FindTypeSerializer(arrayElementType); if (jsonSerializerType == null) { jsonSerializerType = LazyJsonSerializer.FindBuiltInSerializer(arrayElementType); } if (jsonSerializerType != null) { LazyJsonSerializerBase jsonSerializer = (LazyJsonSerializerBase)Activator.CreateInstance(jsonSerializerType); foreach (Object item in dataArray) { jsonArray.TokenList.Add(jsonSerializer.Serialize(item)); } } else { foreach (Object item in dataArray) { jsonArray.TokenList.Add(LazyJsonSerializer.SerializeToken(item)); } } return(jsonArray); }
/// <summary> /// Serialize an object to a json list array token /// </summary> /// <param name="data">The object to be serialized</param> /// <returns>The json list array token</returns> public override LazyJsonToken Serialize(Object data) { if (data == null) { return(new LazyJsonNull()); } Type dataType = data.GetType(); if (dataType.IsGenericType == false || dataType.GetGenericTypeDefinition() != typeof(List <>)) { return(new LazyJsonNull()); } Int32 itemsCount = (Int32)dataType.GetProperties().First(x => x.Name == "Count").GetValue(data); PropertyInfo propertyIndexer = dataType.GetProperties().First(x => x.GetIndexParameters().Length == 1 && x.GetIndexParameters()[0].ParameterType == typeof(Int32)); LazyJsonArray jsonArray = new LazyJsonArray(); Type jsonSerializerType = LazyJsonSerializer.FindTypeSerializer(dataType.GenericTypeArguments[0]); if (jsonSerializerType == null) { jsonSerializerType = LazyJsonSerializer.FindBuiltInSerializer(dataType.GenericTypeArguments[0]); } if (jsonSerializerType != null) { LazyJsonSerializerBase jsonSerializer = (LazyJsonSerializerBase)Activator.CreateInstance(jsonSerializerType); for (int i = 0; i < itemsCount; i++) { jsonArray.TokenList.Add(jsonSerializer.Serialize(propertyIndexer.GetValue(data, new Object[] { i }))); } } else { for (int i = 0; i < itemsCount; i++) { jsonArray.TokenList.Add(LazyJsonSerializer.SerializeToken(propertyIndexer.GetValue(data, new Object[] { i }))); } } return(jsonArray); }
/// <summary> /// Deserialize the json array token to the desired object /// </summary> /// <param name="jsonToken">The json array token to be deserialized</param> /// <param name="dataType">The type of the desired object</param> /// <returns>The desired object instance</returns> public override Object Deserialize(LazyJsonToken jsonToken, Type dataType) { if (jsonToken == null || jsonToken.Type != LazyJsonType.Array || dataType == null || dataType.IsArray == false) { return(null); } LazyJsonArray jsonArray = (LazyJsonArray)jsonToken; Array array = Array.CreateInstance(dataType.GetElementType(), jsonArray.Count); Type arrayElementType = dataType.GetElementType(); Type jsonDeserializerType = LazyJsonDeserializer.FindTypeDeserializer(arrayElementType); if (jsonDeserializerType == null) { jsonDeserializerType = LazyJsonDeserializer.FindBuiltInDeserializer(jsonToken, arrayElementType); } if (jsonDeserializerType != null) { LazyJsonDeserializerBase jsonDeserializer = (LazyJsonDeserializerBase)Activator.CreateInstance(jsonDeserializerType); for (int index = 0; index < jsonArray.Count; index++) { array.SetValue(jsonDeserializer.Deserialize(jsonArray.TokenList[index], arrayElementType), index); } } else { for (int index = 0; index < jsonArray.Count; index++) { array.SetValue(LazyJsonDeserializer.DeserializeToken(jsonArray.TokenList[index], arrayElementType), index); } } return(array); }
/// <summary> /// Deserialize the json list array token to the desired object /// </summary> /// <param name="jsonToken">The json list array token to be deserialized</param> /// <param name="dataType">The type of the desired object</param> /// <returns>The desired object instance</returns> public override Object Deserialize(LazyJsonToken jsonToken, Type dataType) { if (jsonToken == null || jsonToken.Type != LazyJsonType.Array || dataType == null || dataType.IsGenericType == false || dataType.GetGenericTypeDefinition() != typeof(List <>)) { return(null); } LazyJsonArray jsonArray = (LazyJsonArray)jsonToken; Object list = Activator.CreateInstance(dataType); MethodInfo methodAdd = dataType.GetMethods().First(x => x.Name == "Add"); Type jsonDeserializerType = LazyJsonDeserializer.FindTypeDeserializer(dataType.GenericTypeArguments[0]); if (jsonDeserializerType == null) { jsonDeserializerType = LazyJsonDeserializer.FindBuiltInDeserializer(jsonToken, dataType.GenericTypeArguments[0]); } if (jsonDeserializerType != null) { LazyJsonDeserializerBase jsonDeserializer = (LazyJsonDeserializerBase)Activator.CreateInstance(jsonDeserializerType); for (int index = 0; index < jsonArray.Count; index++) { methodAdd.Invoke(list, new Object[] { jsonDeserializer.Deserialize(jsonArray.TokenList[index], dataType.GenericTypeArguments[0]) }); } } else { for (int index = 0; index < jsonArray.Count; index++) { methodAdd.Invoke(list, new Object[] { LazyJsonDeserializer.DeserializeToken(jsonArray.TokenList[index], dataType.GenericTypeArguments[0]) }); } } return(list); }
/// <summary> /// Write an array on the json /// </summary> /// <param name="stringBuilder">The string builder</param> /// <param name="jsonWriterOptions">The json writer options</param> /// <param name="jsonArray">The json array</param> /// <param name="jsonPathType">The json path type</param> private static void WriteArray(StringBuilder stringBuilder, LazyJsonWriterOptions jsonWriterOptions, LazyJsonArray jsonArray, LazyJsonPathType jsonPathType) { String value = String.Empty; String openBracket = "{0}{1}{2}"; String closeBracket = "{0}{1}{2}"; if (jsonWriterOptions.Indent == true) { jsonWriterOptions.IndentLevel++; value = Environment.NewLine + jsonWriterOptions.IndentValue + "{0}"; jsonWriterOptions.IndentLevel--; if (jsonPathType == LazyJsonPathType.ArrayIndex) { openBracket = String.Format(openBracket, Environment.NewLine, jsonWriterOptions.IndentValue, "["); closeBracket = String.Format(closeBracket, Environment.NewLine, jsonWriterOptions.IndentValue, "]"); } else if (jsonPathType == LazyJsonPathType.Property) { openBracket = "["; closeBracket = String.Format(closeBracket, Environment.NewLine, jsonWriterOptions.IndentValue, "]"); } } else { value = "{0}"; openBracket = "["; closeBracket = "]"; } stringBuilder.Append(openBracket); for (int i = 0; i < jsonArray.TokenList.Count; i++) { switch (jsonArray.TokenList[i].Type) { case LazyJsonType.Null: #region LazyJsonType.Null stringBuilder.Append(String.Format(value, "null")); #endregion LazyJsonType.Null break; case LazyJsonType.Boolean: #region LazyJsonType.Boolean LazyJsonBoolean jsonBoolean = (LazyJsonBoolean)jsonArray.TokenList[i]; stringBuilder.Append(String.Format(value, jsonBoolean.Value == null ? "null" : jsonBoolean.Value.ToString().ToLower())); #endregion LazyJsonType.Boolean break; case LazyJsonType.Integer: #region LazyJsonType.Integer LazyJsonInteger jsonInteger = (LazyJsonInteger)jsonArray.TokenList[i]; stringBuilder.Append(String.Format(value, jsonInteger.Value == null ? "null" : jsonInteger.Value)); #endregion LazyJsonType.Integer break; case LazyJsonType.Decimal: #region LazyJsonType.Decimal LazyJsonDecimal jsonDecimal = (LazyJsonDecimal)jsonArray.TokenList[i]; stringBuilder.Append(String.Format(value, jsonDecimal.Value == null ? "null" : jsonDecimal.Value.ToString().Replace(',', '.'))); #endregion LazyJsonType.Decimal break; case LazyJsonType.String: #region LazyJsonType.String LazyJsonString jsonString = (LazyJsonString)jsonArray.TokenList[i]; stringBuilder.Append(String.Format(value, jsonString.Value == null ? "null" : "\"" + jsonString.Value + "\"")); #endregion LazyJsonType.String break; case LazyJsonType.Object: #region LazyJsonType.Object jsonWriterOptions.IndentLevel++; WriteObject(stringBuilder, jsonWriterOptions, (LazyJsonObject)jsonArray.TokenList[i], LazyJsonPathType.ArrayIndex); jsonWriterOptions.IndentLevel--; #endregion LazyJsonType.Object break; case LazyJsonType.Array: #region LazyJsonType.Array jsonWriterOptions.IndentLevel++; WriteArray(stringBuilder, jsonWriterOptions, (LazyJsonArray)jsonArray.TokenList[i], LazyJsonPathType.ArrayIndex); jsonWriterOptions.IndentLevel--; #endregion LazyJsonType.Array break; } stringBuilder.Append(","); } if (stringBuilder[stringBuilder.Length - 1] == ',') { stringBuilder.Remove(stringBuilder.Length - 1, 1); } stringBuilder.Append(closeBracket); }
/// <summary> /// Find token at the specified jPath /// </summary> /// <param name="jPath">The jPath location of the token</param> /// <param name="renameWith">A new property name to the token at specified jPath if it's an object</param> /// <param name="replaceWith">A new token to override the token at specified jPath</param> /// <param name="remove">Indicate if the token should be removed</param> /// <returns>The located token</returns> private LazyJsonToken InternalFind(String jPath, String renameWith = null, LazyJsonToken replaceWith = null, Boolean remove = false) { List <LazyJsonPathNode> pathNodeList = LazyJsonPath.Parse(jPath); if (pathNodeList.Count == 0) { return(null); } LazyJsonToken jsonTokenParent = this.Root; LazyJsonToken jsonToken = null; Int32 pathIndex = 0; if (pathNodeList.Count == 1) { if (replaceWith != null) { this.Root = replaceWith; } return(jsonTokenParent); } pathNodeList.RemoveAt(0); #region Find parent token for (pathIndex = 0; pathIndex < (pathNodeList.Count - 1); pathIndex++) { if (pathNodeList[pathIndex].Type == LazyJsonPathType.Property) { if (jsonTokenParent.Type != LazyJsonType.Object) { return(null); } LazyJsonProperty jsonProperty = ((LazyJsonObject)jsonTokenParent)[((LazyJsonPathProperty)pathNodeList[pathIndex]).Name]; if (jsonProperty == null) { return(null); } jsonTokenParent = jsonProperty.Token; } else if (pathNodeList[pathIndex].Type == LazyJsonPathType.ArrayIndex) { if (jsonTokenParent.Type != LazyJsonType.Array) { return(null); } jsonTokenParent = ((LazyJsonArray)jsonTokenParent)[Convert.ToInt32(((LazyJsonPathArrayIndex)pathNodeList[pathIndex]).Index)]; } } #endregion Find parent token #region Find token if (pathNodeList[pathIndex].Type == LazyJsonPathType.Property) { if (jsonTokenParent.Type != LazyJsonType.Object) { return(null); } LazyJsonObject jsonObjectParent = (LazyJsonObject)jsonTokenParent; LazyJsonProperty jsonProperty = jsonObjectParent[((LazyJsonPathProperty)pathNodeList[pathIndex]).Name]; if (jsonProperty == null) { return(null); } jsonToken = jsonProperty.Token; if (remove == true) { jsonObjectParent.Remove(jsonProperty.Name); } else { if (renameWith != null) { jsonProperty.Name = renameWith; } if (replaceWith != null) { jsonProperty.Token = replaceWith; } } } else if (pathNodeList[pathIndex].Type == LazyJsonPathType.ArrayIndex) { if (jsonTokenParent.Type != LazyJsonType.Array) { return(null); } LazyJsonArray jsonArrayParent = (LazyJsonArray)jsonTokenParent; Int32 index = Convert.ToInt32(((LazyJsonPathArrayIndex)pathNodeList[pathIndex]).Index); jsonToken = jsonArrayParent[index]; if (jsonToken != null) { if (remove == true) { jsonArrayParent.TokenList.RemoveAt(index); } else { if (replaceWith != null) { jsonArrayParent[index] = replaceWith; } } } } #endregion Find token return(jsonToken); }
/// <summary> /// Serialize an object to a json dictionary array token /// </summary> /// <param name="data">The object to be serialized</param> /// <returns>The json dictionary array token</returns> public override LazyJsonToken Serialize(Object data) { if (data == null) { return(new LazyJsonNull()); } Type dataType = data.GetType(); if (dataType.IsGenericType == false || dataType.GetGenericTypeDefinition() != typeof(Dictionary <,>)) { return(new LazyJsonNull()); } Object collection = null; Int32 itemsCount = (Int32)dataType.GetProperties().First(x => x.Name == "Count").GetValue(data); LazyJsonArray jsonArray = new LazyJsonArray(); Array keysArray = Array.CreateInstance(dataType.GenericTypeArguments[0], itemsCount); collection = dataType.GetProperties().First(x => x.Name == "Keys").GetValue(data); collection.GetType().GetMethods().First(x => x.Name == "CopyTo").Invoke(collection, new Object[] { keysArray, 0 }); Array valuesArray = Array.CreateInstance(dataType.GenericTypeArguments[1], itemsCount); collection = dataType.GetProperties().First(x => x.Name == "Values").GetValue(data); collection.GetType().GetMethods().First(x => x.Name == "CopyTo").Invoke(collection, new Object[] { valuesArray, 0 }); Type jsonSerializerType = null; LazyJsonSerializerBase jsonSerializerKey = null; LazyJsonSerializerBase jsonSerializerValue = null; // Keys serializer jsonSerializerType = LazyJsonSerializer.FindTypeSerializer(dataType.GenericTypeArguments[0]); if (jsonSerializerType == null) { jsonSerializerType = LazyJsonSerializer.FindBuiltInSerializer(dataType.GenericTypeArguments[0]); } if (jsonSerializerType != null) { jsonSerializerKey = (LazyJsonSerializerBase)Activator.CreateInstance(jsonSerializerType); } // Values serializer jsonSerializerType = LazyJsonSerializer.FindTypeSerializer(dataType.GenericTypeArguments[1]); if (jsonSerializerType == null) { jsonSerializerType = LazyJsonSerializer.FindBuiltInSerializer(dataType.GenericTypeArguments[1]); } if (jsonSerializerType != null) { jsonSerializerValue = (LazyJsonSerializerBase)Activator.CreateInstance(jsonSerializerType); } for (int i = 0; i < itemsCount; i++) { LazyJsonToken jsonTokenKey = null; LazyJsonToken jsonTokenValue = null; if (jsonSerializerKey != null) { jsonTokenKey = jsonSerializerKey.Serialize(keysArray.GetValue(i)); } else { jsonTokenKey = LazyJsonSerializer.SerializeToken(keysArray.GetValue(i)); } if (jsonSerializerValue != null) { jsonTokenValue = jsonSerializerValue.Serialize(valuesArray.GetValue(i)); } else { jsonTokenValue = LazyJsonSerializer.SerializeToken(valuesArray.GetValue(i)); } LazyJsonArray jsonArrayKeyValuePair = new LazyJsonArray(); jsonArrayKeyValuePair.TokenList.Add(jsonTokenKey); jsonArrayKeyValuePair.TokenList.Add(jsonTokenValue); jsonArray.Add(jsonArrayKeyValuePair); } return(jsonArray); }
/// <summary> /// Append a json token at specified jPath /// </summary> /// <param name="jPath">The jPath location to append the token</param> /// <param name="jsonToken">The json token</param> public void AppendToken(String jPath, LazyJsonToken jsonToken) { if (jPath == "$") { this.Root = jsonToken != null ? jsonToken : new LazyJsonNull(); } else { List <LazyJsonPathNode> jsonPathNodeList = LazyJsonPath.Parse(jPath); String jPathParent = String.Join('.', jsonPathNodeList.ToJsonPathNodeArray(), 0, jsonPathNodeList.Count - 1); LazyJsonToken jsonTokenParent = Find(jPathParent); if (jsonTokenParent != null) { if (jsonPathNodeList[jsonPathNodeList.Count - 1].Type == LazyJsonPathType.ArrayIndex) { if (jsonTokenParent.Type == LazyJsonType.Array) { LazyJsonPathArrayIndex jsonPathArrayIndex = (LazyJsonPathArrayIndex)jsonPathNodeList[jsonPathNodeList.Count - 1]; ((LazyJsonArray)jsonTokenParent)[Convert.ToInt32(jsonPathArrayIndex.Index)] = jsonToken; } else { LazyJsonArray jsonArray = new LazyJsonArray(); jsonArray.Add(jsonToken); Find(jPathParent, replaceWith: jsonArray); } } else if (jsonPathNodeList[jsonPathNodeList.Count - 1].Type == LazyJsonPathType.Property) { if (jsonTokenParent.Type == LazyJsonType.Object) { LazyJsonPathProperty jsonPathProperty = (LazyJsonPathProperty)jsonPathNodeList[jsonPathNodeList.Count - 1]; ((LazyJsonObject)jsonTokenParent)[jsonPathProperty.Name] = new LazyJsonProperty(jsonPathProperty.Name, jsonToken); } else { LazyJsonObject jsonObject = new LazyJsonObject(); jsonObject.Add(new LazyJsonProperty(((LazyJsonPathProperty)jsonPathNodeList[jsonPathNodeList.Count - 1]).Name, jsonToken)); Find(jPathParent, replaceWith: jsonObject); } } } else { if (jsonPathNodeList[jsonPathNodeList.Count - 1].Type == LazyJsonPathType.ArrayIndex) { LazyJsonArray jsonArray = new LazyJsonArray(); jsonArray.Add(jsonToken); AppendToken(jPathParent, jsonArray); } else if (jsonPathNodeList[jsonPathNodeList.Count - 1].Type == LazyJsonPathType.Property) { LazyJsonObject jsonObject = new LazyJsonObject(); jsonObject.Add(new LazyJsonProperty(((LazyJsonPathProperty)jsonPathNodeList[jsonPathNodeList.Count - 1]).Name, jsonToken)); AppendToken(jPathParent, jsonObject); } } } }
/// <summary> /// Append a json array token at specified jPath /// </summary> /// <param name="jPath">The jPath location to append the token</param> /// <param name="jsonArray">The json array token</param> public void AppendArray(String jPath, LazyJsonArray jsonArray) { AppendToken(jPath, jsonArray); }
/// <summary> /// Read a property on the json /// </summary> /// <param name="json">The json</param> /// <param name="jsonReaderOptions">The json reader options</param> /// <param name="jsonReaderResult">The json reader result</param> /// <param name="jsonProperty">The json property</param> /// <param name="line">The current line</param> /// <param name="index">The current index</param> private static void ReadProperty(String json, LazyJsonReaderOptions jsonReaderOptions, LazyJsonReaderResult jsonReaderResult, LazyJsonProperty jsonProperty, ref Int32 line, ref Int32 index) { LazyJsonString jsonStringPropertyName = new LazyJsonString(); ReadString(json, jsonReaderOptions, jsonReaderResult, jsonStringPropertyName, ref line, ref index); jsonProperty.Name = jsonStringPropertyName.Value; if (index < json.Length) { while (index < json.Length) { if (json[index] == ' ') { index++; continue; } if (json[index] == '\r') { index++; continue; } if (json[index] == '\n') { index++; line++; continue; } if (json[index] == '\t') { index++; continue; } if (json[index] == '/') { index++; LazyJsonComments jsonComments = new LazyJsonComments(); ReadComments(json, jsonReaderOptions, jsonReaderResult, jsonComments, ref line, ref index); if (index >= json.Length) { jsonReaderResult.Success = false; jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedCharacter, line, " : ", String.Empty)); } } else if (json[index] == ':') { index++; break; } else { jsonReaderResult.Success = false; jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderInvalidObjectPropertyValue, line, json[index])); index = json.Length; // Exit } } if (index < json.Length) { while (index < json.Length) { if (json[index] == ' ') { index++; continue; } if (json[index] == '\r') { index++; continue; } if (json[index] == '\n') { index++; line++; continue; } if (json[index] == '\t') { index++; continue; } if (json[index] == '/') { index++; LazyJsonComments jsonComments = new LazyJsonComments(); ReadComments(json, jsonReaderOptions, jsonReaderResult, jsonComments, ref line, ref index); if (index >= json.Length) { jsonReaderResult.Success = false; jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedCharacter, line, " : ", String.Empty)); } } else if (json[index] == '[') { index++; LazyJsonArray jsonArray = new LazyJsonArray(); ReadArray(json, jsonReaderOptions, jsonReaderResult, jsonArray, ref line, ref index); jsonProperty.Token = jsonArray; break; } else if (json[index] == '{') { index++; LazyJsonObject jsonObject = new LazyJsonObject(); ReadObject(json, jsonReaderOptions, jsonReaderResult, jsonObject, ref line, ref index); jsonProperty.Token = jsonObject; break; } else if (json[index] == '\"') { index++; LazyJsonString jsonString = new LazyJsonString(); ReadString(json, jsonReaderOptions, jsonReaderResult, jsonString, ref line, ref index); jsonProperty.Token = jsonString; break; } else if (json[index] == '-') { LazyJsonToken jsonToken = null; ReadIntegerOrDecimal(json, jsonReaderOptions, jsonReaderResult, out jsonToken, ref line, ref index); jsonProperty.Token = jsonToken; break; } else if (Char.IsDigit(json[index]) == true) { LazyJsonToken jsonToken = null; ReadIntegerOrDecimal(json, jsonReaderOptions, jsonReaderResult, out jsonToken, ref line, ref index); jsonProperty.Token = jsonToken; break; } else if (Char.IsLetter(json[index]) == true) { LazyJsonToken jsonToken = null; ReadNullOrBoolean(json, jsonReaderOptions, jsonReaderResult, out jsonToken, ref line, ref index); jsonProperty.Token = jsonToken; break; } else { jsonReaderResult.Success = false; jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderInvalidObjectPropertyValue, line, json[index])); index = json.Length; // Exit } } } else { jsonReaderResult.Success = false; jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderInvalidObjectPropertyValue, line, String.Empty)); } } else { jsonReaderResult.Success = false; jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderInvalidObjectPropertyValue, line, String.Empty)); } }
/// <summary> /// Read the root node of the json /// </summary> /// <param name="json">The json</param> /// <param name="jsonReaderOptions">The json reader options</param> /// <param name="lazyJson">The lazy json</param> private static void ReadRoot(String json, LazyJsonReaderOptions jsonReaderOptions, LazyJson lazyJson) { Int32 line = 1; Int32 index = 0; while (index < json.Length) { if (json[index] == ' ') { index++; continue; } if (json[index] == '\r') { index++; continue; } if (json[index] == '\n') { index++; line++; continue; } if (json[index] == '\t') { index++; continue; } if (json[index] == '/') { index++; LazyJsonComments jsonComments = new LazyJsonComments(); ReadComments(json, jsonReaderOptions, lazyJson.ReaderResult, jsonComments, ref line, ref index); if (index >= json.Length && jsonComments.IsInBlock == true && jsonComments.IsInBlockComplete == false) { lazyJson.ReaderResult.Success = false; lazyJson.ReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedCharacter, line, "*/", String.Empty)); } } else if (json[index] == '[') { index++; LazyJsonArray jsonArray = new LazyJsonArray(); ReadArray(json, jsonReaderOptions, lazyJson.ReaderResult, jsonArray, ref line, ref index); if (lazyJson.ReaderResult.Success == true) { lazyJson.Root = jsonArray; } break; } else if (json[index] == '{') { index++; LazyJsonObject jsonObject = new LazyJsonObject(); ReadObject(json, jsonReaderOptions, lazyJson.ReaderResult, jsonObject, ref line, ref index); if (lazyJson.ReaderResult.Success == true) { lazyJson.Root = jsonObject; } break; } else if (json[index] == '\"') { index++; LazyJsonString jsonString = new LazyJsonString(); ReadString(json, jsonReaderOptions, lazyJson.ReaderResult, jsonString, ref line, ref index); if (lazyJson.ReaderResult.Success == true) { lazyJson.Root = jsonString; } break; } else if (json[index] == '-') { LazyJsonToken jsonToken = null; ReadIntegerOrDecimal(json, jsonReaderOptions, lazyJson.ReaderResult, out jsonToken, ref line, ref index); if (lazyJson.ReaderResult.Success == true) { lazyJson.Root = jsonToken; } break; } else if (Char.IsDigit(json[index]) == true) { LazyJsonToken jsonToken = null; ReadIntegerOrDecimal(json, jsonReaderOptions, lazyJson.ReaderResult, out jsonToken, ref line, ref index); if (lazyJson.ReaderResult.Success == true) { lazyJson.Root = jsonToken; } break; } else if (Char.IsLetter(json[index]) == true) { LazyJsonToken jsonToken = null; ReadNullOrBoolean(json, jsonReaderOptions, lazyJson.ReaderResult, out jsonToken, ref line, ref index); if (lazyJson.ReaderResult.Success == true) { lazyJson.Root = jsonToken; } break; } else { lazyJson.Root = new LazyJsonNull(); lazyJson.ReaderResult.Success = false; lazyJson.ReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedCharacter, line, "//", json[index])); index = json.Length; // Exit break; } } while (index < json.Length) { if (json[index] == ' ') { index++; continue; } if (json[index] == '\r') { index++; continue; } if (json[index] == '\n') { index++; line++; continue; } if (json[index] == '\t') { index++; continue; } if (json[index] == '/') { index++; LazyJsonComments jsonComments = new LazyJsonComments(); ReadComments(json, jsonReaderOptions, lazyJson.ReaderResult, jsonComments, ref line, ref index); if (index >= json.Length && jsonComments.IsInBlock == true && jsonComments.IsInBlockComplete == false) { lazyJson.ReaderResult.Success = false; lazyJson.ReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedCharacter, line, "*/", String.Empty)); } } else { lazyJson.Root = new LazyJsonNull(); lazyJson.ReaderResult.Success = false; lazyJson.ReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedCharacter, line, "//", json[index])); index = json.Length; // Exit break; } } }
/// <summary> /// Deserialize the json dictionary array token to the desired object /// </summary> /// <param name="jsonToken">The json dictionary array token to be deserialized</param> /// <param name="dataType">The type of the desired object</param> /// <returns>The desired object instance</returns> public override Object Deserialize(LazyJsonToken jsonToken, Type dataType) { if (jsonToken == null || jsonToken.Type != LazyJsonType.Array || dataType == null || dataType.IsGenericType == false || dataType.GetGenericTypeDefinition() != typeof(Dictionary <,>)) { return(null); } LazyJsonArray jsonArray = (LazyJsonArray)jsonToken; Object dictionary = Activator.CreateInstance(dataType); MethodInfo methodAdd = dataType.GetMethods().First(x => x.Name == "Add"); Type jsonDeserializerType = null; LazyJsonDeserializerBase jsonDeserializerKey = null; LazyJsonDeserializerBase jsonDeserializerValue = null; // Keys deserializer jsonDeserializerType = LazyJsonDeserializer.FindTypeDeserializer(dataType.GenericTypeArguments[0]); if (jsonDeserializerType == null) { jsonDeserializerType = LazyJsonDeserializer.FindBuiltInDeserializer(jsonToken, dataType.GenericTypeArguments[0]); } if (jsonDeserializerType != null) { jsonDeserializerKey = (LazyJsonDeserializerBase)Activator.CreateInstance(jsonDeserializerType); } // Values deserializer jsonDeserializerType = LazyJsonDeserializer.FindTypeDeserializer(dataType.GenericTypeArguments[1]); if (jsonDeserializerType == null) { jsonDeserializerType = LazyJsonDeserializer.FindBuiltInDeserializer(jsonToken, dataType.GenericTypeArguments[1]); } if (jsonDeserializerType != null) { jsonDeserializerValue = (LazyJsonDeserializerBase)Activator.CreateInstance(jsonDeserializerType); } for (int index = 0; index < jsonArray.Count; index++) { if (jsonArray.TokenList[index].Type != LazyJsonType.Array) { continue; } LazyJsonArray jsonArrayKeyValuePair = (LazyJsonArray)jsonArray.TokenList[index]; if (jsonArrayKeyValuePair.TokenList.Count != 2) { continue; } Object key = null; Object value = null; if (jsonDeserializerKey != null) { key = jsonDeserializerKey.Deserialize(jsonArrayKeyValuePair.TokenList[0], dataType.GenericTypeArguments[0]); } else { key = LazyJsonDeserializer.DeserializeToken(jsonArrayKeyValuePair.TokenList[0], dataType.GenericTypeArguments[0]); } if (jsonDeserializerValue != null) { value = jsonDeserializerValue.Deserialize(jsonArrayKeyValuePair.TokenList[1], dataType.GenericTypeArguments[1]); } else { value = LazyJsonDeserializer.DeserializeToken(jsonArrayKeyValuePair.TokenList[1], dataType.GenericTypeArguments[1]); } methodAdd.Invoke(dictionary, new Object[] { key, value }); } return(dictionary); }