/// <summary> /// Serialize an object to a json dataset object token /// </summary> /// <param name="data">The object to be serialized</param> /// <returns>The json dataset object token</returns> public override LazyJsonToken Serialize(Object data) { if (data == null || data is not DataSet) { return(new LazyJsonNull()); } DataSet dataSet = (DataSet)data; LazyJsonObject jsonObjectDataSet = new LazyJsonObject(); // Name jsonObjectDataSet.Add(new LazyJsonProperty("Name", new LazyJsonString(dataSet.DataSetName))); // Tables LazyJsonObject jsonObjectDataSetTables = new LazyJsonObject(); jsonObjectDataSet.Add(new LazyJsonProperty("Tables", jsonObjectDataSetTables)); LazyJsonSerializerDataTable jsonSerializerDataTable = new LazyJsonSerializerDataTable(); foreach (DataTable dataTable in dataSet.Tables) { LazyJsonToken jsonTokenDataSetTable = jsonSerializerDataTable.Serialize(dataTable); if (jsonTokenDataSetTable != null) { jsonObjectDataSetTables.Add(new LazyJsonProperty(dataTable.TableName, jsonTokenDataSetTable)); } } return(jsonObjectDataSet); }
/// <summary> /// Deserialize the json token to the desired object /// </summary> /// <param name="jsonToken">The json token to be deserialized</param> /// <param name="dataType">The type of the desired object</param> /// <returns>The desired object instance</returns> public static Object DeserializeToken(LazyJsonToken jsonToken, Type dataType) { if (jsonToken == null || jsonToken.Type == LazyJsonType.Null || dataType == null) { return(null); } Type jsonDeserializerType = FindTypeDeserializer(dataType); if (jsonDeserializerType == null) { jsonDeserializerType = FindBuiltInDeserializer(jsonToken, dataType); } if (jsonDeserializerType != null) { return(((LazyJsonDeserializerBase)Activator.CreateInstance(jsonDeserializerType)).Deserialize(jsonToken, dataType)); } else { if (jsonToken.Type != LazyJsonType.Object) { return(null); } return(DeserializeObject((LazyJsonObject)jsonToken, dataType)); } }
/// <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> /// Deserialize the json dataset token to the desired object /// </summary> /// <param name="jsonToken">The json dataset 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(DataSet)) { return(null); } LazyJsonObject jsonObjectDataSet = (LazyJsonObject)jsonToken; DataSet dataSet = new DataSet(); // Name if (jsonObjectDataSet["Name"] != null && jsonObjectDataSet["Name"].Token.Type == LazyJsonType.String) { dataSet.DataSetName = ((LazyJsonString)jsonObjectDataSet["Name"].Token).Value; } // Tables if (jsonObjectDataSet["Tables"] != null && jsonObjectDataSet["Tables"].Token.Type == LazyJsonType.Object) { LazyJsonObject jsonObjectDataSetTables = (LazyJsonObject)jsonObjectDataSet["Tables"].Token; LazyJsonDeserializerDataTable jsonDeserializerDataTable = new LazyJsonDeserializerDataTable(); foreach (LazyJsonProperty jsonPropertyDataSetTable in jsonObjectDataSetTables.PropertyList) { DataTable dataTable = jsonDeserializerDataTable.Deserialize <DataTable>(jsonPropertyDataSetTable); if (dataTable != null) { dataSet.Tables.Add(dataTable); } } } return(dataSet); }
/// <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> /// Deserialize the json datarow token to the desired object /// </summary> /// <param name="jsonToken">The json datarow token</param> /// <param name="dataTable">The datatable</param> public void Deserialize(LazyJsonToken jsonToken, DataTable dataTable) { LazyJsonObject jsonObjectDataRow = (LazyJsonObject)jsonToken; DataRow dataRow = dataTable.NewRow(); dataTable.Rows.Add(dataRow); dataRow.AcceptChanges(); // State DataRowState dataRowState = DataRowState.Unchanged; if (jsonObjectDataRow["State"] != null && jsonObjectDataRow["State"].Token.Type == LazyJsonType.String) { switch (((LazyJsonString)jsonObjectDataRow["State"].Token).Value.ToLower()) { case "added": dataRowState = DataRowState.Added; break; case "modified": dataRowState = DataRowState.Modified; break; case "deleted": dataRowState = DataRowState.Deleted; break; } } // Values if (jsonObjectDataRow["Values"] != null && jsonObjectDataRow["Values"].Token.Type == LazyJsonType.Object) { LazyJsonObject jsonObjectDataRowValues = (LazyJsonObject)jsonObjectDataRow["Values"].Token; // Values Original if (dataRowState == DataRowState.Modified) { if (jsonObjectDataRowValues["Original"] != null && jsonObjectDataRowValues["Original"].Token.Type == LazyJsonType.Object) { DeserializeDataRow((LazyJsonObject)jsonObjectDataRowValues["Original"].Token, dataRow); } dataRow.AcceptChanges(); } // Values Current if (jsonObjectDataRowValues["Current"] != null && jsonObjectDataRowValues["Current"].Token.Type == LazyJsonType.Object) { DeserializeDataRow((LazyJsonObject)jsonObjectDataRowValues["Current"].Token, dataRow); } } switch (dataRowState) { case DataRowState.Added: dataRow.AcceptChanges(); dataRow.SetAdded(); break; case DataRowState.Deleted: dataRow.AcceptChanges(); dataRow.Delete(); break; case DataRowState.Unchanged: dataRow.AcceptChanges(); break; } }
/// <summary> /// Deserialize the json string token to the desired object /// </summary> /// <param name="jsonToken">The json string 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 || dataType == null) { return(null); } if (jsonToken.Type == LazyJsonType.Null) { if (dataType == typeof(Char)) { return('\0'); } if (dataType == typeof(String)) { return(null); } if (dataType == typeof(Object)) { return(null); } if (dataType == typeof(Nullable <Char>)) { return(null); } } else if (jsonToken.Type == LazyJsonType.String) { LazyJsonString jsonString = (LazyJsonString)jsonToken; if (dataType == typeof(Char)) { return(jsonString.Value == null ? '\0' : Convert.ToChar(jsonString.Value)); } if (dataType == typeof(String)) { return(jsonString.Value); } if (dataType == typeof(Object)) { return(jsonString.Value); } if (dataType == typeof(Nullable <Char>)) { return(jsonString.Value == null ? null : Convert.ToChar(jsonString.Value)); } } return(null); }
/// <summary> /// Deserialize the json boolean token to the desired object /// </summary> /// <param name="jsonToken">The json boolean 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 || dataType == null) { return(null); } if (jsonToken.Type == LazyJsonType.Null) { if (dataType == typeof(Boolean)) { return(false); } if (dataType == typeof(Object)) { return(false); } if (dataType == typeof(Nullable <Boolean>)) { return(null); } } else if (jsonToken.Type == LazyJsonType.Boolean) { LazyJsonBoolean jsonBoolean = (LazyJsonBoolean)jsonToken; if (dataType == typeof(Boolean)) { return(jsonBoolean.Value == null ? false : Convert.ToBoolean(jsonBoolean.Value)); } if (dataType == typeof(Object)) { return(jsonBoolean.Value == null ? false : Convert.ToBoolean(jsonBoolean.Value)); } if (dataType == typeof(Nullable <Boolean>)) { return(jsonBoolean.Value); } } return(null); }
/// <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> /// Find built in deserializer for the json token or the type /// </summary> /// <param name="jsonToken">The json token to be deserialized</param> /// <param name="dataType">The data type to look for</param> /// <returns>The type of the deserializer</returns> public static Type FindBuiltInDeserializer(LazyJsonToken jsonToken, Type dataType) { if (dataType == null) { return(null); } if (jsonToken.Type == LazyJsonType.Boolean) { return(typeof(LazyJsonDeserializerBoolean)); } if (jsonToken.Type == LazyJsonType.Integer) { return(typeof(LazyJsonDeserializerInteger)); } if (jsonToken.Type == LazyJsonType.Decimal) { return(typeof(LazyJsonDeserializerDecimal)); } if (jsonToken.Type == LazyJsonType.String) { return(typeof(LazyJsonDeserializerString)); } if (dataType == typeof(Boolean)) { return(typeof(LazyJsonDeserializerBoolean)); } if (dataType == typeof(Char)) { return(typeof(LazyJsonDeserializerString)); } if (dataType == typeof(String)) { return(typeof(LazyJsonDeserializerString)); } if (dataType == typeof(Int16)) { return(typeof(LazyJsonDeserializerInteger)); } if (dataType == typeof(Int32)) { return(typeof(LazyJsonDeserializerInteger)); } if (dataType == typeof(Int64)) { return(typeof(LazyJsonDeserializerInteger)); } if (dataType == typeof(float)) { return(typeof(LazyJsonDeserializerDecimal)); } if (dataType == typeof(Double)) { return(typeof(LazyJsonDeserializerDecimal)); } if (dataType == typeof(Decimal)) { return(typeof(LazyJsonDeserializerDecimal)); } if (dataType == typeof(Nullable <Boolean>)) { return(typeof(LazyJsonDeserializerBoolean)); } if (dataType == typeof(Nullable <Char>)) { return(typeof(LazyJsonDeserializerString)); } if (dataType == typeof(Nullable <Int16>)) { return(typeof(LazyJsonDeserializerInteger)); } if (dataType == typeof(Nullable <Int32>)) { return(typeof(LazyJsonDeserializerInteger)); } if (dataType == typeof(Nullable <Int64>)) { return(typeof(LazyJsonDeserializerInteger)); } if (dataType == typeof(Nullable <float>)) { return(typeof(LazyJsonDeserializerDecimal)); } if (dataType == typeof(Nullable <Double>)) { return(typeof(LazyJsonDeserializerDecimal)); } if (dataType == typeof(Nullable <Decimal>)) { return(typeof(LazyJsonDeserializerDecimal)); } if (dataType.IsArray == true) { return(typeof(LazyJsonDeserializerArray)); } if (dataType.IsGenericType == true && dataType.GetGenericTypeDefinition() == typeof(List <>)) { return(typeof(LazyJsonDeserializerList)); } if (dataType.IsGenericType == true && dataType.GetGenericTypeDefinition() == typeof(Dictionary <,>)) { return(typeof(LazyJsonDeserializerDictionary)); } if (dataType == typeof(DataSet)) { return(typeof(LazyJsonDeserializerDataSet)); } if (dataType == typeof(DataTable)) { return(typeof(LazyJsonDeserializerDataTable)); } return(null); }
/// <summary> /// Append a token at root /// </summary> /// <param name="jsonToken">The json token</param> public void AppendRoot(LazyJsonToken jsonToken) { AppendToken("$", jsonToken); }
/// <summary> /// Deserialize the json integer token to the desired object /// </summary> /// <param name="jsonToken">The json integer 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 || dataType == null) { return(null); } if (jsonToken.Type == LazyJsonType.Null) { if (dataType == typeof(Int16)) { return(0); } if (dataType == typeof(Int32)) { return(0); } if (dataType == typeof(Int64)) { return(0); } if (dataType == typeof(Object)) { return(0); } if (dataType == typeof(Nullable <Int16>)) { return(null); } if (dataType == typeof(Nullable <Int32>)) { return(null); } if (dataType == typeof(Nullable <Int64>)) { return(null); } } else if (jsonToken.Type == LazyJsonType.Integer) { LazyJsonInteger jsonInteger = (LazyJsonInteger)jsonToken; if (dataType == typeof(Int16)) { return(jsonInteger.Value == null ? 0 : Convert.ToInt16(jsonInteger.Value)); } if (dataType == typeof(Int32)) { return(jsonInteger.Value == null ? 0 : Convert.ToInt32(jsonInteger.Value)); } if (dataType == typeof(Int64)) { return(jsonInteger.Value == null ? 0 : Convert.ToInt64(jsonInteger.Value)); } if (dataType == typeof(Object)) { return(jsonInteger.Value == null ? 0 : Convert.ToInt64(jsonInteger.Value)); } if (dataType == typeof(Nullable <Int16>)) { return(jsonInteger.Value == null ? null : Convert.ToInt16(jsonInteger.Value)); } if (dataType == typeof(Nullable <Int32>)) { return(jsonInteger.Value == null ? null : Convert.ToInt32(jsonInteger.Value)); } if (dataType == typeof(Nullable <Int64>)) { return(jsonInteger.Value == null ? null : Convert.ToInt64(jsonInteger.Value)); } } return(null); }
/// <summary> /// Append a json property at specified jPath object /// </summary> /// <param name="jPath">The jPath object location to append the property</param> /// <param name="name">The property name</param> /// <param name="jsonToken">The json property token</param> public void AppendProperty(String jPath, String name, LazyJsonToken jsonToken) { AppendToken(String.Join('.', jPath, name != null ? name : LazyJson.UNNAMED_PROPERTY), jsonToken); }
/// <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> /// 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> /// <returns>The located token</returns> public LazyJsonToken Find(String jPath, String renameWith = null, LazyJsonToken replaceWith = null) { return(InternalFind(jPath, renameWith: renameWith, replaceWith: replaceWith)); }
/// <summary> /// Read an integer or decimal 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="jsonToken">The json token</param> /// <param name="line">The current line</param> /// <param name="index">The current index</param> private static void ReadIntegerOrDecimal(String json, LazyJsonReaderOptions jsonReaderOptions, LazyJsonReaderResult jsonReaderResult, out LazyJsonToken jsonToken, ref Int32 line, ref Int32 index) { Int32 startIndex = index; if (json[index] == '-') { index++; if (index == json.Length || Char.IsDigit(json[index]) == false) { jsonToken = new LazyJsonNull(); jsonReaderResult.Success = false; jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedToken, line, "0-9", index < json.Length ? json[index] : String.Empty)); index = json.Length; // Exit return; } } while (index < json.Length && Char.IsDigit(json[index]) == true) { index++; } if (index < json.Length && json[index] == '.') { index++; while (index < json.Length && Char.IsDigit(json[index]) == true) { index++; } } String token = json.Substring(startIndex, index - startIndex); if (token.Contains('.') == true) { if (token.Substring(token.IndexOf('.') + 1).Length > 0) { jsonToken = new LazyJsonDecimal(Convert.ToDecimal(token.Replace('.', ','))); } else { jsonToken = new LazyJsonNull(); jsonReaderResult.Success = false; jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedToken, line, "0-9", index < json.Length ? json[index] : String.Empty)); index = json.Length; // Exit } } else { jsonToken = new LazyJsonInteger(Convert.ToInt64(token)); } }
/// <summary> /// Deserialize the json token to the desired object /// </summary> /// <typeparam name="T">The type of the desired object</typeparam> /// <param name="jsonToken">The json token to be deserialized</param> /// <returns>The desired object instance</returns> public T Deserialize <T>(LazyJsonToken jsonToken) { return((T)Deserialize(jsonToken, typeof(T))); }
/// <summary> /// Deserialize the json token to the desired object /// </summary> /// <typeparam name="T">The type of the desired object</typeparam> /// <param name="jsonToken">The json token to be deserialized</param> /// <returns>The desired object instance</returns> public static T DeserializeToken <T>(LazyJsonToken jsonToken) { return((T)DeserializeToken(jsonToken, typeof(T))); }
/// <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> /// Add a new token to the array /// </summary> /// <param name="token">The json token</param> public void Add(LazyJsonToken token) { this.TokenList.Add(token != null ? token : new LazyJsonNull()); }
/// <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> /// Add a new property to the object /// </summary> /// <param name="name">The property name</param> /// <param name="jsonToken">The json token</param> public void Add(String name, LazyJsonToken jsonToken) { this[name] = new LazyJsonProperty(name, jsonToken); }
public LazyJsonProperty(String name, LazyJsonToken token) { this.Name = name; this.Token = token; }
/// <summary> /// Deserialize the json token to the desired object /// </summary> /// <param name="jsonToken">The json token to be deserialized</param> /// <param name="dataType">The type of the desired object</param> /// <returns>The desired object instance</returns> public abstract Object Deserialize(LazyJsonToken jsonToken, Type dataType);
/// <summary> /// Deserialize the json decimal token to the desired object /// </summary> /// <param name="jsonToken">The json decimal 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 || dataType == null) { return(null); } if (jsonToken.Type == LazyJsonType.Null) { if (dataType == typeof(float)) { return(0); } if (dataType == typeof(Double)) { return(0); } if (dataType == typeof(Decimal)) { return(0); } if (dataType == typeof(Object)) { return(0); } if (dataType == typeof(Nullable <float>)) { return(null); } if (dataType == typeof(Nullable <Double>)) { return(null); } if (dataType == typeof(Nullable <Decimal>)) { return(null); } } else if (jsonToken.Type == LazyJsonType.Decimal) { LazyJsonDecimal jsonDecimal = (LazyJsonDecimal)jsonToken; if (dataType == typeof(float)) { return(jsonDecimal.Value == null ? 0 : (float)jsonDecimal.Value); } if (dataType == typeof(Double)) { return(jsonDecimal.Value == null ? 0 : Convert.ToDouble(jsonDecimal.Value)); } if (dataType == typeof(Decimal)) { return(jsonDecimal.Value == null ? 0 : Convert.ToDecimal(jsonDecimal.Value)); } if (dataType == typeof(Object)) { return(jsonDecimal.Value == null ? 0 : Convert.ToDecimal(jsonDecimal.Value)); } if (dataType == typeof(Nullable <float>)) { return(jsonDecimal.Value == null ? null : (float)jsonDecimal.Value); } if (dataType == typeof(Nullable <Double>)) { return(jsonDecimal.Value == null ? null : Convert.ToDouble(jsonDecimal.Value)); } if (dataType == typeof(Nullable <Decimal>)) { return(jsonDecimal.Value == null ? null : Convert.ToDecimal(jsonDecimal.Value)); } } return(null); }
/// <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 null or boolean 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="jsonToken">The json token</param> /// <param name="line">The current line</param> /// <param name="index">The current index</param> private static void ReadNullOrBoolean(String json, LazyJsonReaderOptions jsonReaderOptions, LazyJsonReaderResult jsonReaderResult, out LazyJsonToken jsonToken, ref Int32 line, ref Int32 index) { Int32 startIndex = index; while (index < json.Length && Char.IsLetter(json[index]) == true) { index++; } String token = json.Substring(startIndex, index - startIndex); if (token.ToLower() == "null") { jsonToken = new LazyJsonNull(); if (jsonReaderOptions.CaseSensitive == true && token != "null") { jsonReaderResult.Success = false; jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedToken, line, "null", token)); index = json.Length; // Exit } } else if (token.ToLower() == "true") { jsonToken = new LazyJsonBoolean(true); if (jsonReaderOptions.CaseSensitive == true && token != "true") { jsonReaderResult.Success = false; jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedToken, line, "true", token)); index = json.Length; // Exit } } else if (token.ToLower() == "false") { jsonToken = new LazyJsonBoolean(false); if (jsonReaderOptions.CaseSensitive == true && token != "false") { jsonReaderResult.Success = false; jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedToken, line, "false", token)); index = json.Length; // Exit } } else { jsonToken = new LazyJsonNull(); jsonReaderResult.Success = false; jsonReaderResult.ErrorList.Add(String.Format(Properties.Resources.LazyJsonReaderUnexpectedToken, line, "null|true|false", token)); index = json.Length; // Exit } }
/// <summary> /// Write the root node of the json /// </summary> /// <param name="stringBuilder">The string builder</param> /// <param name="jsonWriterOptions">The json writer options</param> /// <param name="jsonToken">The root token</param> private static void WriteRoot(StringBuilder stringBuilder, LazyJsonWriterOptions jsonWriterOptions, LazyJsonToken jsonToken) { switch (jsonToken.Type) { case LazyJsonType.Null: #region LazyJsonType.Null stringBuilder.Append("null"); #endregion LazyJsonType.Null break; case LazyJsonType.Boolean: #region LazyJsonType.Boolean LazyJsonBoolean jsonBoolean = (LazyJsonBoolean)jsonToken; stringBuilder.Append(jsonBoolean.Value == null ? "null" : jsonBoolean.Value.ToString().ToLower()); #endregion LazyJsonType.Boolean break; case LazyJsonType.Integer: #region LazyJsonType.Integer LazyJsonInteger jsonInteger = (LazyJsonInteger)jsonToken; stringBuilder.Append(jsonInteger.Value == null ? "null" : jsonInteger.Value); #endregion LazyJsonType.Integer break; case LazyJsonType.Decimal: #region LazyJsonType.Decimal LazyJsonDecimal jsonDecimal = (LazyJsonDecimal)jsonToken; stringBuilder.Append(jsonDecimal.Value == null ? "null" : jsonDecimal.Value.ToString().Replace(',', '.')); #endregion LazyJsonType.Decimal break; case LazyJsonType.String: #region LazyJsonType.String LazyJsonString jsonString = (LazyJsonString)jsonToken; stringBuilder.Append(jsonString.Value == null ? "null" : "\"" + jsonString.Value + "\""); #endregion LazyJsonType.String break; case LazyJsonType.Object: #region LazyJsonType.Object WriteObject(stringBuilder, jsonWriterOptions, (LazyJsonObject)jsonToken, LazyJsonPathType.Property); #endregion LazyJsonType.Object break; case LazyJsonType.Array: #region LazyJsonType.Array WriteArray(stringBuilder, jsonWriterOptions, (LazyJsonArray)jsonToken, LazyJsonPathType.Property); #endregion LazyJsonType.Array break; } }
/// <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; } } }