public void Rowset_FromJSON_DefMissed(bool rowsAsMap) { var row = new Person { Name = "Henry", Age = 43 }; var rowSet = new Rowset(row.Schema); rowSet.Add(row); var options = new Azos.Serialization.JSON.JsonWritingOptions { RowsetMetadata = true, RowsAsMap = rowsAsMap }; var json = rowSet.ToJson(options); var map = JsonReader.DeserializeDataObject(json) as JsonDataMap; var schema = (map["Schema"] as IDictionary <string, object>); var defs = schema["FieldDefs"] as IList <object>; defs.RemoveAt(1); bool allMatched; var trg = RowsetBase.FromJSON(map, out allMatched); Aver.IsFalse(allMatched); var trgRow = trg[0]; Aver.AreEqual(trgRow.Schema.FieldCount, 1); Aver.AreObjectsEqual(trgRow["Name"], "Henry"); }
/// <summary> /// Writes JSON data to the byte[] /// </summary> public static byte[] WriteToBuffer(object data, JsonWritingOptions options = null, Encoding encoding = null) { using (var ms = new MemoryStream()) { Write(data, ms, options, encoding); return(ms.ToArray()); } }
/// <summary> /// Appends JSON representation of an IEnumerable /// </summary> public static void WriteArray(TextWriter wri, IEnumerable data, int level, JsonWritingOptions options) { if (options == null) { options = JsonWritingOptions.Compact; } writeArray(wri, data, level, options); }
/// <summary> /// Appends JSON data into the instance of StringBuilder /// </summary> public static void Write(object data, TextWriter wri, JsonWritingOptions options = null) { if (options == null) { options = JsonWritingOptions.Compact; } writeAny(wri, data, 0, options); }
/// <summary> /// Appends JSON representation of a map(IDictionary) /// </summary> public static void WriteMap(TextWriter wri, IDictionary data, int level, JsonWritingOptions options = null) { if (options == null) { options = JsonWritingOptions.Compact; } writeMap(wri, data, level, options); }
/// <summary> /// Appends JSON representation of a map(IEnumerable(DictionaryEntry)) /// </summary> public static void WriteMap(TextWriter wri, int level, JsonWritingOptions options, params DictionaryEntry[] data) { if (options == null) { options = JsonWritingOptions.Compact; } writeMap(wri, data, level, options); }
/// <summary> /// Writes a string in JSON format (a la "JSON encode string") - using quotes and escaping charecters that need it /// </summary> /// <param name="wri">TextWriter instance to append data into</param> /// <param name="data">Original string to encode as JSON</param> /// <param name="opt">JSONWriting options instance, if omitted then JSONWritingOptions.Compact is used</param> public static void EncodeString(TextWriter wri, string data, JsonWritingOptions opt = null) { if (data.IsNullOrEmpty()) { wri.Write("\"\""); return; } if (opt == null) { opt = JsonWritingOptions.Compact; } wri.Write('"'); for (int i = 0; i < data.Length; i++) { char c = data[i]; if (c > 127 && opt.ASCIITarget) { wri.Write("\\u"); wri.Write(((int)c).ToString("x4")); continue; } switch (c) { case '\\': { wri.Write(@"\\"); break; } case '/': { wri.Write(@"\/"); break; } case (char)CharCodes.Char0: { wri.Write(@"\u0000"); break; } case (char)CharCodes.AlertBell: { wri.Write(@"\u"); ((int)c).ToString("x4"); break; } case (char)CharCodes.Backspace: { wri.Write(@"\b"); break; } case (char)CharCodes.Formfeed: { wri.Write(@"\f"); break; } case (char)CharCodes.LF: { wri.Write(@"\n"); break; } case (char)CharCodes.CR: { wri.Write(@"\r"); break; } case (char)CharCodes.Tab: { wri.Write(@"\t"); break; } case (char)CharCodes.VerticalQuote: { wri.Write(@"\u"); ((int)c).ToString("x4"); break; } case '"': { wri.Write(@"\"""); break; } default: { wri.Write(c); break; } } }//for wri.Write('"'); }
private static void indent(TextWriter wri, int level, JsonWritingOptions opt) { if (opt.IndentWidth == 0) { return; } var total = level * opt.IndentWidth; for (var i = 0; i < total; i++) { wri.Write(' '); } }
public void JSON_SerializeRowset_TypedRows() { var rowset = new Rowset(Schema.GetForTypedDoc(typeof(Person))); for (var i = 0; i < 10; i++) { rowset.Insert(new Person { ID = "POP{0}".Args(i), FirstName = "Oleg", LastName = "Popov-{0}".Args(i), DOB = new DateTime(1953, 12, 10), YearsInSpace = 12 }); } // Serialization without schema var json = rowset.ToJson(Azos.Serialization.JSON.JsonWritingOptions.PrettyPrint); json.See(); var rowset2 = json.JsonToDynamic(); Aver.AreEqual("Popov-1", rowset2.Rows[1][2]); RowsetBase rowset3 = new Rowset(Schema.GetForTypedDoc(typeof(Person))); var res = Rowset.FromJSON <Person>(json, ref rowset3); Aver.AreEqual(10, res); Aver.AreEqual(10, rowset3.Count); Aver.AreObjectsEqual("Popov-1", rowset3[1][2]); var options = new Azos.Serialization.JSON.JsonWritingOptions { RowsetMetadata = true, IndentWidth = 2, ObjectLineBreak = true, MemberLineBreak = true, SpaceSymbols = true, ASCIITarget = false }; rowset3.Clear(); var json2 = rowset.ToJson(options); var res2 = Rowset.FromJSON <Person>(json2, ref rowset3); Aver.AreEqual(10, res); Aver.AreEqual(10, rowset3.Count); Aver.AreObjectsEqual("Popov-1", rowset3[1][2]); }
public void Table_FromJSON_ShemaOnly() { var src = new Table(new TeztRow().Schema); var options = new Azos.Serialization.JSON.JsonWritingOptions { RowsetMetadata = true, SpaceSymbols = true, IndentWidth = 2, MemberLineBreak = true, ObjectLineBreak = true }; var json = src.ToJson(options); var trg = RowsetBase.FromJSON(json, true); schemaAssertions(trg.Schema, src.Schema); Aver.AreEqual(trg.Count, 0); }
/// <summary> /// Writes JSON data to the string /// </summary> public static string Write(object data, JsonWritingOptions options = null, IFormatProvider formatProvider = null) { if (options == null) { options = JsonWritingOptions.CompactRowsAsMap; } var sb = new StringBuilder(0xff); using (var wri = formatProvider == null ? new StringWriter(sb) : new StringWriter(sb, formatProvider)) { writeAny(wri, data, 0, options); } return(sb.ToString()); }
private static void writeArray(TextWriter wri, IEnumerable data, int level, JsonWritingOptions opt) { wri.Write('['); var first = true; foreach (var elm in data) { if (!first) { wri.Write(opt.SpaceSymbols ? ", " : ","); } writeAny(wri, elm, level + 1, opt); first = false; } wri.Write(']'); }
/// <summary> /// Writes NLSMap either as a dict or as a {n:"", d: ""} pair as Options.NLSMapLanguageISO filter dictates /// </summary> void IJsonWritable.WriteAsJson(TextWriter wri, int nestingLevel, JsonWritingOptions options) { if (m_Data == null) { wri.Write("{}"); return; } if (options == null || options.Purpose == JsonSerializationPurpose.Marshalling || options.NLSMapLanguageISO.IsZero) { JsonWriter.WriteMap(wri, nestingLevel, options, m_Data.Select ( e => new System.Collections.DictionaryEntry(e.ISO.Value, e) ).ToArray()); return; } var pair = this[options.NLSMapLanguageISO]; if (!pair.IsAssigned && options.NLSMapLanguageISODefault != options.NLSMapLanguageISO) { pair = this[options.NLSMapLanguageISODefault]; } if (pair.IsAssigned) { JsonWriter.WriteMap(wri, nestingLevel, options, new System.Collections.DictionaryEntry("n", pair.Name), new System.Collections.DictionaryEntry("d", pair.Description)); } else { JsonWriter.WriteMap(wri, nestingLevel, options, new System.Collections.DictionaryEntry("n", null), new System.Collections.DictionaryEntry("d", null)); } }
/// <summary> /// Writes NLSMap either as a dict or as a {n:"", d: ""} pair as Options.NLSMapLanguageISO filter dictates /// </summary> void IJsonWritable.WriteAsJson(TextWriter wri, int nestingLevel, JsonWritingOptions options = null) { if (m_Data == null) { wri.Write("{}"); return; } if (options == null || options.Purpose == JsonSerializationPurpose.Marshalling || options.NLSMapLanguageISO.IsNullOrWhiteSpace()) { JsonWriter.WriteMap(wri, nestingLevel, options, m_Data.Select ( e => new System.Collections.DictionaryEntry(IOUtils.UnpackISO3CodeFromInt(e.ISO), e) ).ToArray()); return; } var pair = this[options.NLSMapLanguageISO]; if (!pair.IsAssigned && !options.NLSMapLanguageISODefault.EqualsOrdIgnoreCase(options.NLSMapLanguageISO)) { pair = this[options.NLSMapLanguageISODefault]; } if (pair.IsAssigned) { JsonWriter.WriteMap(wri, nestingLevel, options, new System.Collections.DictionaryEntry("n", pair.Name), new System.Collections.DictionaryEntry("d", pair.Description)); } else { JsonWriter.WriteMap(wri, nestingLevel, options, new System.Collections.DictionaryEntry("n", null), new System.Collections.DictionaryEntry("d", null)); } }
public JsonWritingOptions(JsonWritingOptions other) { if (other == null) { return; } this.NLSMapLanguageISO = other.NLSMapLanguageISO; this.NLSMapLanguageISODefault = other.NLSMapLanguageISODefault; this.IndentWidth = other.IndentWidth; this.SpaceSymbols = other.SpaceSymbols; this.ObjectLineBreak = other.ObjectLineBreak; this.MemberLineBreak = other.MemberLineBreak; this.ASCIITarget = other.ASCIITarget; this.ISODates = other.ISODates; this.MaxNestingLevel = other.MaxNestingLevel; this.RowsAsMap = other.RowsAsMap; this.RowsetMetadata = other.RowsetMetadata; this.Purpose = other.Purpose; this.MapSkipNulls = other.MapSkipNulls; this.RowMapTargetName = other.RowMapTargetName; }
/// <summary> /// Writes a string in JSON format (a la "JSON encode string") - using quotes and escaping characters that need it /// </summary> /// <param name="wri">TextWriter instance to append data into</param> /// <param name="data">Original string to encode as JSON</param> /// <param name="opt">JSONWriting options instance, if omitted then JSONWritingOptions.Compact is used</param> /// <param name="utcOffset">UTC offset override. If not supplied then offset form local time zone is used</param> public static void EncodeDateTime(TextWriter wri, DateTime data, JsonWritingOptions opt = null, TimeSpan?utcOffset = null) { if (opt == null) { opt = JsonWritingOptions.Compact; } if (!opt.ISODates) { wri.Write("new Date({0})".Args(data.ToMillisecondsSinceUnixEpochStart())); return; } wri.Write('"'); var year = data.Year; if (year > 999) { wri.Write(year); } else if (year > 99) { wri.Write('0'); wri.Write(year); } else if (year > 9) { wri.Write("00"); wri.Write(year); } else if (year > 0) { wri.Write("000"); wri.Write(year); } wri.Write('-'); var month = data.Month; if (month > 9) { wri.Write(month); } else { wri.Write('0'); wri.Write(month); } wri.Write('-'); var day = data.Day; if (day > 9) { wri.Write(day); } else { wri.Write('0'); wri.Write(day); } wri.Write('T'); var hour = data.Hour; if (hour > 9) { wri.Write(hour); } else { wri.Write('0'); wri.Write(hour); } wri.Write(':'); var minute = data.Minute; if (minute > 9) { wri.Write(minute); } else { wri.Write('0'); wri.Write(minute); } wri.Write(':'); var second = data.Second; if (second > 9) { wri.Write(second); } else { wri.Write('0'); wri.Write(second); } var ms = data.Millisecond; if (ms > 0) { wri.Write('.'); if (ms > 99) { wri.Write(ms); } else if (ms > 9) { wri.Write('0'); wri.Write(ms); } else { wri.Write("00"); wri.Write(ms); } } if (data.Kind == DateTimeKind.Utc) { wri.Write('Z'); } else { var offset = utcOffset == null?TimeZoneInfo.Local.GetUtcOffset(data) : utcOffset.Value; wri.Write(offset.Ticks < 0 ? '-' : '+'); hour = Math.Abs(offset.Hours); if (hour > 9) { wri.Write(hour); } else { wri.Write('0'); wri.Write(hour); } wri.Write(':'); minute = Math.Abs(offset.Minutes); if (minute > 9) { wri.Write(minute); } else { wri.Write('0'); wri.Write(minute); } } wri.Write('"'); }
private static void writeMap(TextWriter wri, IEnumerable <DictionaryEntry> data, int level, JsonWritingOptions opt) { if (level > 0) { level++; } if (opt.ObjectLineBreak) { wri.WriteLine(); indent(wri, level, opt); } wri.Write('{'); //20200615 DKh #304 if (opt.MapSortKeys) { data = data.OrderBy(kvp => (kvp.Key?.ToString()).Default(string.Empty), StringComparer.Ordinal); } var first = true; foreach (DictionaryEntry entry in data) { if (opt.MapSkipNulls) { if (entry.Value == null) { continue; } // NLSMap is a special type which is treated as a ref type for perf optimization if (entry.Value is NLSMap && ((NLSMap)entry.Value).Count == 0) { continue; } } if (!first) { wri.Write(opt.SpaceSymbols ? ", " : ","); } if (opt.MemberLineBreak) { wri.WriteLine(); indent(wri, level + 1, opt); } EncodeString(wri, entry.Key.ToString(), opt); wri.Write(opt.SpaceSymbols ? ": " : ":"); writeAny(wri, entry.Value, level + 1, opt); first = false; } if (!first && opt.MemberLineBreak) { wri.WriteLine(); indent(wri, level, opt); } wri.Write('}'); }
public void Rowset_FromJSON(bool rowsAsMap) { var row = new TeztRow(); var src = new Rowset(row.Schema); row.BoolField = true; row.CharField = 'a'; row.StringField = "aaa"; row.DateTimeField = new DateTime(2016, 1, 2); row.GDIDField = new GDID(1, 2, 3); row.ByteField = 100; row.ShortField = -100; row.IntField = -999; row.UIntField = 254869; row.LongField = -267392; row.FloatField = 32768.32768F; row.DoubleField = -1048576.1048576D; row.DecimalField = 1.0529M; row.NullableField = null; row.ArrayInt = new int[] { -1, 0, 1 }; row.ListString = new List <string> { "one", "two", "three" }; row.DictionaryIntStr = new Dictionary <int, string> { { 1, "first" }, { 2, "second" } }; row.RowField = new Person { Name = "John", Age = 20 }; src.Add(row); row.BoolField = false; row.CharField = 'b'; row.StringField = "bbb"; row.DateTimeField = new DateTime(2016, 2, 1); row.GDIDField = new GDID(4, 5, 6); row.ByteField = 101; row.ShortField = 100; row.IntField = 999; row.UIntField = 109876; row.LongField = 267392; row.FloatField = -32768.32768F; row.DoubleField = -048576.1048576D; row.DecimalField = -1.0529M; row.NullableField = null; row.ArrayInt = new int[] { 1, 0, -1 }; row.ListString = new List <string> { "three", "two", "one" }; row.DictionaryIntStr = new Dictionary <int, string> { { 0, "zero" }, { 1, "first" }, { 2, "second" } }; row.RowField = new Person { Name = "Ann", Age = 19 }; src.Add(row); var options = new Azos.Serialization.JSON.JsonWritingOptions { RowsetMetadata = true, SpaceSymbols = true, IndentWidth = 2, MemberLineBreak = true, ObjectLineBreak = true, RowsAsMap = rowsAsMap }; var json = src.ToJson(options); var trg = RowsetBase.FromJSON(json); schemaAssertions(trg.Schema, src.Schema); rowsAssertions(src, trg, rowsAsMap); }
void IJsonWritable.WriteAsJson(TextWriter wri, int nestingLevel, JsonWritingOptions options) { JsonWriter.WriteMap(wri, nestingLevel, options, new System.Collections.DictionaryEntry("n", Name), new System.Collections.DictionaryEntry("d", Description)); }
private static void writeMap(TextWriter wri, IDictionary data, int level, JsonWritingOptions opt) { writeMap(wri, new dictEnumberable(data), level, opt); }
/// <summary> /// Writes JSON data to the stream /// </summary> public static void Write(object data, Stream stream, JsonWritingOptions options = null, Encoding encoding = null) { using (var writer = new StreamWriter(stream, encoding ?? UTF8Encoding.UTF8)) Write(data, writer, options); }
private static void writeAny(TextWriter wri, object data, int level, JsonWritingOptions opt) { if (data == null) { wri.Write("null"); //do NOT LOCALIZE! return; } if (level > opt.MaxNestingLevel) { throw new JSONSerializationException(StringConsts.JSON_SERIALIZATION_MAX_NESTING_EXCEEDED_ERROR.Args(opt.MaxNestingLevel)); } if (data is string) { EncodeString(wri, (string)data, opt); return; } if (data is bool) //the check is here for speed { wri.Write(((bool)data) ? "true" : "false"); //do NOT LOCALIZE! return; } if (data is int || data is long) //the check is here for speed { wri.Write(((IConvertible)data).ToString(System.Globalization.CultureInfo.InvariantCulture)); return; } if (data is double || data is float || data is decimal) //the check is here for speed { wri.Write(((IConvertible)data).ToString(System.Globalization.CultureInfo.InvariantCulture)); return; } if (data is DateTime) { EncodeDateTime(wri, (DateTime)data, opt); return; } if (data is TimeSpan) { var ts = (TimeSpan)data; wri.Write(ts.Ticks); return; } if (data is Guid) { var guid = (Guid)data; wri.Write('"'); wri.Write(guid.ToString("D")); wri.Write('"'); return; } if (data is IJsonWritable) //these types know how to directly write themselves { ((IJsonWritable)data).WriteAsJson(wri, level, opt); return; } if (data is JsonDynamicObject) //unwrap dynamic { writeAny(wri, ((JsonDynamicObject)data).Data, level, opt); return; } if (data is IDictionary) //must be BEFORE IEnumerable { writeMap(wri, (IDictionary)data, level, opt); return; } if (data is IEnumerable) { writeArray(wri, (IEnumerable)data, level, opt); return; } var tdata = data.GetType(); if (tdata.IsPrimitive || tdata.IsEnum) { string val; if (data is IConvertible) { val = ((IConvertible)data).ToString(System.Globalization.CultureInfo.InvariantCulture); } else { val = data.ToString(); } EncodeString(wri, val, opt); return; } var fields = SerializationUtils.GetSerializableFields(tdata); var dict = fields.Select( f => { var name = f.Name; var iop = name.IndexOf('<'); if (iop >= 0) //handle anonymous type field name { var icl = name.IndexOf('>'); if (icl > iop + 1) { name = name.Substring(iop + 1, icl - iop - 1); } } return(new DictionaryEntry(name, f.GetValue(data))); }); //select writeMap(wri, dict, level, opt); }
/// <summary> /// Serializes object into JSON format using provided TextWriter /// </summary> public static void ToJson(this object root, TextWriter wri, JsonWritingOptions options = null) { JsonWriter.Write(root, wri, options); }
/// <summary> /// Serializes object into JSON string /// </summary> public static string ToJson(this object root, JsonWritingOptions options = null) { return(JsonWriter.Write(root, options)); }
/// <summary> /// Writes JSON data to the file /// </summary> public static void WriteToFile(object data, string fileName, JsonWritingOptions options = null, Encoding encoding = null) { using (var fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) Write(data, fs, options, encoding); }
/// <summary> /// Serializes object into JSON format using provided stream and optional encoding /// </summary> public static void ToJson(this object root, Stream stream, JsonWritingOptions options = null, Encoding encoding = null) { JsonWriter.Write(root, stream, options, encoding); }
private static void writeMap(TextWriter wri, IEnumerable <DictionaryEntry> data, int level, JsonWritingOptions opt) { if (level > 0) { level++; } if (opt.ObjectLineBreak) { wri.WriteLine(); indent(wri, level, opt); } wri.Write('{'); var first = true; foreach (DictionaryEntry entry in data) { //20160324 DKh if (opt.MapSkipNulls) { if (entry.Value == null) { continue; } // 20161009 Dkh, Ogee NLSMap is a special type which is treated as a ref type for perf optimization if (entry.Value is NLSMap && ((NLSMap)entry.Value).Count == 0) { continue; } } if (!first) { wri.Write(opt.SpaceSymbols ? ", " : ","); } if (opt.MemberLineBreak) { wri.WriteLine(); indent(wri, level + 1, opt); } EncodeString(wri, entry.Key.ToString(), opt); wri.Write(opt.SpaceSymbols ? ": " : ":"); writeAny(wri, entry.Value, level + 1, opt); first = false; } if (!first && opt.MemberLineBreak) { wri.WriteLine(); indent(wri, level, opt); } wri.Write('}'); }