/// <summary> /// Froms the json. /// 从byte数组获取JsonDocument /// </summary> /// <returns>The json.返回JsonDocument对象</returns> /// <param name="json">Json.Json数组</param> /// <param name="encod">Encod.编码,默认使用当前默认编码</param> public static JsonDocument FromJson(byte[] json, JsonEncoding encod = JsonEncoding.Default) { switch (encod) { case JsonEncoding.ASCII: return(FromString(Encoding.ASCII.GetString(json))); case JsonEncoding.Unicode: return(FromString(Encoding.Unicode.GetString(json))); case JsonEncoding.UTF7: return(FromString(Encoding.UTF7.GetString(json))); case JsonEncoding.UTF8: return(FromString(Encoding.UTF8.GetString(json))); case JsonEncoding.UTF32: return(FromString(Encoding.UTF32.GetString(json))); case JsonEncoding.BigEndianUnicode: return(FromString(Encoding.BigEndianUnicode.GetString(json))); case JsonEncoding.Default: default: return(FromString(Encoding.Default.GetString(json))); } }
/// <summary> /// Create encoder /// </summary> /// <param name="stream"></param> /// <param name="context"></param> /// <param name="encoding"></param> /// <param name="formatting"></param> public JsonEncoderEx(Stream stream, ServiceMessageContext context = null, JsonEncoding encoding = JsonEncoding.Object, Newtonsoft.Json.Formatting formatting = Newtonsoft.Json.Formatting.None) : this(new StreamWriter(stream, new UTF8Encoding(false)), context, encoding, formatting) { }
/// <summary> /// Create encoder /// </summary> /// <param name="writer"></param> /// <param name="context"></param> /// <param name="encoding"></param> /// <param name="formatting"></param> public JsonEncoderEx(TextWriter writer, ServiceMessageContext context = null, JsonEncoding encoding = JsonEncoding.Object, Newtonsoft.Json.Formatting formatting = Newtonsoft.Json.Formatting.None) : this(new JsonTextWriter(writer) { AutoCompleteOnClose = true, DateFormatHandling = DateFormatHandling.IsoDateFormat, FloatFormatHandling = FloatFormatHandling.String, Formatting = formatting }, context, encoding) { }
/// <summary> /// Create encoder /// </summary> /// <param name="writer"></param> /// <param name="context"></param> /// <param name="encoding"></param> public JsonEncoderEx(JsonWriter writer, ServiceMessageContext context = null, JsonEncoding encoding = JsonEncoding.Object) { _namespaces = new Stack <string>(); Context = context ?? new ServiceMessageContext(); _writer = writer ?? throw new ArgumentNullException(nameof(writer)); _encoding = encoding; switch (encoding) { case JsonEncoding.Object: _writer.WriteStartObject(); break; case JsonEncoding.Array: _writer.WriteStartArray(); break; } }
/// <summary> /// Froms the string. /// 从Json字符串获取JsonDocument /// </summary> /// <returns>The Json.返回JsonDocument对象</returns> /// <param name="jsonstr">Jsonstr.Json字符串</param> /// <param name="encod">Encod.编码,默认使用当前默认编码</param> public static JsonDocument FromString(string jsonstr, JsonEncoding encod = JsonEncoding.Default) { JsonDocument doc = new JsonDocument(); doc.Encod = encod; if (jsonstr == null) { throw new NullReferenceException("不能为null"); } int index = 0; if (jsonstr[0] == '{') { doc.Object = GetObject(ref index, jsonstr).ToObject(); } else if (jsonstr[0] == '[') { doc.Array = GetArray(ref index, jsonstr).ToArray(); } return(doc); }
public JsonSerializationTestContext() { _encoding = JsonEncoding.UTF16LE; _fieldNameResolver = new CamelCaseFieldNameResolver(); }