public override string ToJson(JsonSerializationOptions options = null) { var _options = new JsonSerializationOptions(options); var indent = _options.CurrentIndent + _options.Indent; var crlf = Environment.NewLine; var currentIndent = _options.CurrentIndent; if (!_options.UseIndentation) { indent = ""; currentIndent = ""; crlf = ""; } var result = new StringBuilder(); var c = 0; result.Append("{"); foreach (var item in dictionary) { var prefix = ((c++ == 0)?"":", "); result.AppendFormat("{0}\"{1}\": \"{2}\"", prefix, item.Key, item.Value); } result.Append("}"); return(result.ToString()); }
public static string ToJsonList(this IEnumerable <JsonModel> list, JsonSerializationOptions options = null) { var _options = new JsonSerializationOptions(options); var first = true; var sb = new StringBuilder(); var schema = ""; var skipSchema = false; foreach (var item in list) { var jl = item.ToJsonList(skipSchema, skipSchema, _options); if (first) { schema = jl.Key; first = false; skipSchema = true; } sb.AppendWithCommaIfNotEmpty(jl.Value); } return(string.Format("{{\"Schema\": {0},\"Data\":[{1}]}}", schema, sb)); }
public virtual string ToJson(JsonSerializationOptions options = null) { var _options = new JsonSerializationOptions(options); var indent = _options.CurrentIndent + _options.Indent; var crlf = Environment.NewLine; var currentIndent = _options.CurrentIndent; if (!_options.UseIndentation) { indent = ""; currentIndent = ""; crlf = ""; } var sb = new StringBuilder(); sb.AppendFormat("\"Category\":\"{0}\"", Category); sb.AppendFormatWithComma("\"Name\":\"{0}\"", Name); sb.AppendFormatWithComma("\"Id\":{0}", Id); sb.AppendFormatWithCommaIfNotEmpty("\"Title\":\"{0}\"", Title); sb.AppendFormatWithCommaIfNotEmpty("\"Description\":\"{0}\"", Description); sb.AppendFormatWithCommaIfNotEmpty("\"Code\":\"{0}\"", Code); sb.AppendFormatWithCommaIf("\"Parent\":\"{0}\"", Parent != 0, Parent); return("{" + sb + "}"); }
public string ToJson(JsonSerializationOptions options = null) { /* * var settings = new JsonSerializerSettings(); * * settings.NullValueHandling = NullValueHandling.Ignore; * settings.DefaultValueHandling = DefaultValueHandling.Ignore; * * var result = JsonConvert.SerializeObject(this, settings); * * return result; */ var _options = options ?? new JsonSerializationOptions(); var childOption = new JsonSerializationOptions(_options); var newLine = _options.UseIndentation ? Environment.NewLine : ""; var indent = _options.UseIndentation ? _options.CurrentIndent + _options.Indent : ""; var sb = new StringBuilder(); foreach (var msg in messages) { var comma = sb.Length == 0 ? "," : ""; sb.Append($"{msg.ToJson(childOption)}{comma}{newLine}"); } return($"{_options.CurrentIndent}[{newLine}{sb}{_options.CurrentIndent}]"); }
/// <summary> /// Creates new JSON result from the object. Status code will be set to 200. /// </summary> protected IActionResult Json(object obj, JsonSerializationOptions options = null) { JsonResult json = new JsonResult(); json.Options = options ?? Mvc.JsonOutputOptions; json.Set(obj); return(json); }
public string ToJson(JsonSerializationOptions options = null) { var _innerResponses = ""; if (HasInnerResponses()) { var sb = new StringBuilder(); foreach (var res in InnerResponses) { if (sb.Length == 0) { sb.Append(",\"InnerResponses\": [" + res.Value.ToJson()); } else { sb.Append("," + res.Value.ToJson()); } } sb.Append("]"); _innerResponses = sb.ToString(); } var exception = ""; if (Exception != null) { exception = ",\"Exception\": " + JsonConvert.SerializeObject(Exception); } var info = ""; if (Info != null) { info = ",\"Info\": " + JsonConvert.SerializeObject(Info); } var data = ""; var prop = this.GetType().GetProperty("Data"); if (prop != null) { var dataValue = prop.GetValue(this, null); if (dataValue != null) { data = ",\"Data\": " + JsonConvert.SerializeObject(dataValue); } } var result = $@"{{""Success"": {Success.ToString().ToLower()},""Status"": ""{Status}"", ""Date"":""{Date:yyyy/MM/dd HH:mm:ss.fffffff}"",""Message"": ""{HttpUtility.JavaScriptStringEncode(Message)}""{data}{info}{exception}{_innerResponses}}}"; return(result); }
public string ToJson(JsonSerializationOptions options = null) { var _options = options ?? new JsonSerializationOptions(); var childOption = new JsonSerializationOptions(_options); var newLine = _options.UseIndentation ? Environment.NewLine : ""; var indent = _options.UseIndentation ? _options.CurrentIndent + _options.Indent : ""; var sb = new StringBuilder(); foreach (var item in items) { var comma = sb.Length == 0 ? "," : ""; sb.Append($"\"{item.Key}\":\"{HttpUtility.JavaScriptStringEncode(item.Value?.ToString() ?? "")}\"{comma}{newLine}"); } return($"{_options.CurrentIndent}{{{newLine}{sb}{_options.CurrentIndent}}}"); }
/// <summary> /// Sets json objects of json result /// </summary> public async Task SetAsync(object model) { if (Options == null) { Options = new JsonSerializationOptions(); } if (Options.UseNewtonsoft) { string serialized = Options.NewtonsoftOptions != null ? JsonConvert.SerializeObject(model, Options.NewtonsoftOptions) : JsonConvert.SerializeObject(model); Stream = new MemoryStream(Encoding.UTF8.GetBytes(serialized)); } else { Stream = new MemoryStream(); await System.Text.Json.JsonSerializer.SerializeAsync(Stream, model, model.GetType(), Options.SystemTextOptions); } }
public override string ToJson(JsonSerializationOptions options = null) { var sb = new StringBuilder(); if (Items != null) { foreach (var item in Items) { var baseModel = item as IJsonSerializable; if (baseModel != null) { sb.AppendWithComma(baseModel.ToJson()); } else { sb.AppendWithComma(JsonConvert.SerializeObject(item)); } } } return(string.Format("{{\"Count\":{0}", Count) + ",\"Items\":[" + sb + "]}"); }
public virtual string ToJson(JsonSerializationOptions options = null) { /* * var settings = new JsonSerializerSettings(); * * settings.NullValueHandling = NullValueHandling.Ignore; * settings.DefaultValueHandling = DefaultValueHandling.Ignore; * * var result = JsonConvert.SerializeObject(this, settings); * * return result; */ var _options = options ?? new JsonSerializationOptions(); var newLine = _options.UseIndentation ? Environment.NewLine : ""; var indent = _options.UseIndentation ? _options.CurrentIndent + _options.Indent : ""; var sDate = $"{indent}\"{nameof(Date)}\":\"{Date.ToUniversalTime()}\"{newLine}"; var sOrder = $"{indent},\"{nameof(Order)}\":{Order}{newLine}"; var sDepth = $"{indent},\"{nameof(Depth)}\":{Depth}{newLine}"; var sType = $"{indent},\"{nameof(Type)}\":\"{Type}\"{newLine}"; var sSource = $"{indent},\"{nameof(Source)}\":\"{Source}\"{newLine}"; var sCategory = string.IsNullOrEmpty(Category) ? "" : $"{indent},\"{nameof(Category)}\":\"{HttpUtility.JavaScriptStringEncode(Category)}\"{newLine}"; var sOperation = string.IsNullOrEmpty(Operation) ? "" : $"{indent},\"{nameof(Operation)}\":\"{HttpUtility.JavaScriptStringEncode(Operation)}\"{newLine}"; var sCode = string.IsNullOrEmpty(Code) ? "" : $"{indent},\"{nameof(Code)}\":\"{HttpUtility.JavaScriptStringEncode(Code)}\"{newLine}"; var sInfo = string.IsNullOrEmpty(Info) ? "" : $"{indent},\"{nameof(Info)}\":\"{HttpUtility.JavaScriptStringEncode(Info)}\"{newLine}"; var sException = (Exception == null) ? "" : $"\"{nameof(Exception)}\":{JsonConvert.SerializeObject(Exception)}{newLine}"; var sCaller = string.IsNullOrEmpty(Caller) ? "" : $"{indent},\"{nameof(Caller)}\":\"{HttpUtility.JavaScriptStringEncode(Caller)}\"{newLine}"; var sFilePath = string.IsNullOrEmpty(FilePath) ? "" : $"{indent},\"{nameof(FilePath)}\":\"{HttpUtility.JavaScriptStringEncode(FilePath)}\"{newLine}"; var sLine = Line == 0 ? "" : $"{indent},\"{nameof(Line)}\":{Line}{newLine}"; var sRawText = string.IsNullOrEmpty(RawText) ? "" : $"{indent},\"{nameof(RawText)}\":\"{HttpUtility.JavaScriptStringEncode(RawText)}\"{newLine}"; var sArgs = (Args == null || Args.Count == 0) ? "" : $"\"{nameof(Args)}\":{Args.ToJson()}{newLine}"; var sText = string.IsNullOrEmpty(Text) ? "" : $"{indent},\"{nameof(Text)}\":\"{HttpUtility.JavaScriptStringEncode(Text)}\"{newLine}"; return ($@"{_options.CurrentIndent}{{{newLine}{sDate}{sOrder}{sDepth}{sType}{sSource}{sCategory}{sOperation}{sCode}{sInfo}{sException}{sCaller}{sFilePath}{sLine}{sRawText}{sArgs}{sText}{_options.CurrentIndent}}}"); }
/// <summary> /// Creates new JSON result from the object. Status code will be set to 200. /// </summary> protected async Task <IActionResult> JsonAsync(object obj, HttpStatusCode statusCode = HttpStatusCode.OK, JsonSerializationOptions options = null) { JsonResult json = new JsonResult(statusCode); json.Options = options ?? Mvc.JsonOutputOptions; await json.SetAsync(obj); return(json); }
public string ToJson(JsonSerializationOptions options = null) { return(ToJson(_context: null)); }
public static string ToJson(this IBaseServiceResponse response, JsonSerializationOptions options = null) { var _options = new JsonSerializationOptions(options); var result = new StringBuilder(); if (response != null) { foreach (var prop in response.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)) { if (prop.CanRead) { var propType = prop.PropertyType; var value = prop.GetValue(response); if (value != null) { var cmdParam = value as CommandParameter; if (cmdParam != null) { result.AppendFormatWithCommaIfNotEmpty("\"{0}\": {1}", prop.Name, cmdParam.ToJson()); continue; } if (propType.IsEnum) { result.AppendFormatWithCommaIfNotEmpty("\"{0}\": \"{1}\"", prop.Name, value.ToString()); continue; } if (propType == TypeHelper.TypeOfBool) { result.AppendFormatWithCommaIfNotEmpty("\"{0}\": {1}", prop.Name, SafeClrConvert.ToBoolean(value) ? "true" : "false"); continue; } if (propType == TypeHelper.TypeOfString) { result.AppendFormatWithCommaIfNotEmpty("\"{0}\": \"{1}\"", prop.Name, value); continue; } if (propType == TypeHelper.TypeOfChar) { result.AppendFormatWithCommaIfNotEmpty("\"{0}\": '{1}'", prop.Name, value); continue; } if (propType.IsBasicType()) { result.AppendFormatWithCommaIfNotEmpty("\"{0}\": {1}", prop.Name, value); continue; } if (value is Guid) { if ((Guid)value != Guid.Empty) { result.AppendFormatWithCommaIfNotEmpty("\"{0}\":'{1}'", prop.Name, value); continue; } else { result.AppendFormatWithCommaIfNotEmpty("\"{0}\":null", prop.Name); continue; } } var jm = value as IJsonSerializable; if (jm != null) { result.AppendFormatWithCommaIfNotEmpty("\"{0}\":{1}", prop.Name, jm.ToJson(_options)); continue; } var e = value as IEnumerable; if (e != null) { result.AppendFormatWithCommaIfNotEmpty("\"{0}\": ", prop.Name); result.Append(e.ToJson(_options)); continue; } result.AppendFormatWithCommaIfNotEmpty("\"{0}\": \"{1}\"", prop.Name, value.ToString()); } else { result.AppendFormatWithCommaIfNotEmpty("\"{0}\": null", prop.Name); } } } } return("{" + result + "}"); }
public static string ToJson(this IEnumerable arr, JsonSerializationOptions options) { var result = new StringBuilder(); var c = 0; result.Append("["); if (arr != null) { foreach (var item in arr) { c++; var value = item; if (c > 1) { result.Append(", "); } if (value == null) { result.Append("null"); continue; } var valueType = value.GetType(); if (valueType == TypeHelper.TypeOfBool) { result.Append(value.ToString().ToLower()); continue; } if (valueType.IsNumeric()) { result.Append(value); continue; } if (valueType == TypeHelper.TypeOfChar) { result.AppendFormat("'{0}'", value); continue; } if (valueType == TypeHelper.TypeOfGuid) { if ((Guid)(object)(value) != Guid.Empty) { result.AppendFormat("\"{0}\"", value); continue; } else { result.AppendFormat("\"{0}\"", "null"); continue; } } if (valueType == TypeHelper.TypeOfString) { result.AppendFormat("\"{0}\"", HttpUtility.JavaScriptStringEncode(value.ToString())); continue; } var jm = value as IJsonSerializable; if (jm != null) { result.Append(jm.ToJson(options)); continue; } var e = value as IEnumerable; if (e != null) { result.Append(e.ToJson(options)); continue; } result.AppendFormat("\"{0}\"", value); } } result.Append("]"); return(result.ToString()); }
public virtual string ToJson(JsonSerializationOptions options = null) { var _options = new JsonSerializationOptions(options); var result = new StringBuilder(); foreach (var x in GetProperties()) { var value = x.Value; if (value == null) { result.AppendFormatWithCommaIfNotEmpty("\"{0}\":{1}", x.Key, "null"); continue; } if (value is bool) { result.AppendFormatWithCommaIfNotEmpty("\"{0}\":{1}", x.Key, value.ToString().ToLower()); continue; } if (value.GetType().IsNumeric()) { result.AppendFormatWithCommaIfNotEmpty("\"{0}\":{1}", x.Key, value); continue; } if (value is char) { result.AppendFormatWithCommaIfNotEmpty("\"{0}\":'{1}'", x.Key, value); continue; } if (value is Guid) { if ((Guid)value != Guid.Empty) { result.AppendFormatWithCommaIfNotEmpty("\"{0}\":'{1}'", x.Key, value); continue; } else { result.AppendFormatWithCommaIfNotEmpty("\"{0}\":null", x.Key); continue; } } if (value is string) { result.AppendFormatWithCommaIfNotEmpty("\"{0}\":\"{1}\"", x.Key, HttpUtility.JavaScriptStringEncode(value.ToString())); continue; } var jm = value as IJsonSerializable; if (jm != null) { result.AppendFormatWithCommaIfNotEmpty("\"{0}\":{1}", x.Key, jm.ToJson(_options)); continue; } var e = value as IEnumerable; if (e != null) { result.AppendFormatWithCommaIfNotEmpty("\"{0}\": ", x.Key); result.Append(e.ToJson(_options)); continue; } result.AppendFormatWithCommaIfNotEmpty("\"{0}\":\"{1}\"", x.Key, value); } return("{" + result + "}"); }
public KeyValuePair <string, string> ToJsonList(bool skipSchema = false, bool skipChildSchema = true, JsonSerializationOptions options = null) { var _options = new JsonSerializationOptions(options); var result = new StringBuilder(); var properties = new StringBuilder(); foreach (var x in GetProperties()) { if (!skipSchema) { properties.AppendFormatWithCommaIfNotEmpty("\"{0}\"", x.Key); } var value = x.Value; if (value == null) { result.AppendWithCommaIfNotEmpty("null"); continue; } if (value is bool) { result.AppendWithCommaIfNotEmpty(value.ToString().ToLower()); continue; } if (value.GetType().IsNumeric()) { result.AppendWithCommaIfNotEmpty(value.ToString()); continue; } if (value is char) { result.AppendFormatWithCommaIfNotEmpty("'{0}'", value); continue; } if (value is string) { if (value != null) { result.AppendFormatWithCommaIfNotEmpty("\"{0}\"", HttpUtility.JavaScriptStringEncode(value.ToString())); } else { result.AppendWithCommaIfNotEmpty(""); } continue; } var jm = value as JsonModel; if (jm != null) { var jl = jm.ToJsonList(skipSchema, skipChildSchema, _options); if (!skipChildSchema) { properties.AppendWithCommaIfNotEmpty(jl.Key); } result.AppendWithCommaIfNotEmpty(jl.Value); //result.AppendFormatWithCommaIfNotEmpty("{{\"Schema\":[{0}],\"Data\":{1}}}", jl.Key, jl.Value); continue; } var e = value as IEnumerable; if (e != null) { result.AppendWithCommaIfNotEmpty(e.ToJson(_options)); continue; } result.AppendWithCommaIfNotEmpty(value.ToString()); } return(new KeyValuePair <string, string>("[" + properties + "]", "[" + result + "]")); }