internal static void WritePair(Stream s, AnsonField af, Type ftype, Type vtype, string n, object v, JsonOpt opt = null) { // is this ignored? if (af != null && af.ignoreTo) { return; } Utils.WriteByt(s, ',', ' '); // prop Utils.WriteStr(s, n, opt == null || opt.quotKey); Utils.WriteByt(s, ':', ' '); // value if (af != null && af.refer == AnsonField.enclosing) { Utils.WriteStr(s, vtype.FullName, true); return; } try { if (!ftype.IsPrimitive) { Type vclz = v?.GetType(); WriteNonPrimitive(s, vclz, v, opt); } else if (ftype.IsPrimitive) { Utils.WriteStr(s, (string)v); } } catch (Exception x) { throw new AnsonException(0, x.Message); } }
public IJsonable ToBlock(Stream stream, JsonOpt opt = null) { bool quotK = opt == null || opt.quotKey; Type type = GetType(); Utils.WriteByt(stream, '{'); Utils.WriteStr(stream, "type", quotK); Utils.WriteByt(stream, ':', ' '); Utils.WriteStr(stream, GetType().FullName, true); Hashtable fmap = new Hashtable(); Hashtable pmap = new Hashtable(); JSONAnsonListener.MergeFields(type, fmap, pmap); foreach (FieldInfo f in fmap.Values) { if (f.Name.EndsWith("BackingField")) { continue; // properties backing field } AnsonField af = (AnsonField)Attribute.GetCustomAttribute(f, typeof(AnsonField)); object v = f.GetValue(this); WritePair(stream, af, f.GetType(), f.FieldType, f.Name, v, opt); } foreach (PropertyInfo p in pmap.Values) { AnsonField af = (AnsonField)Attribute.GetCustomAttribute(p, typeof(AnsonField)); object v = p.GetValue(this); WritePair(stream, af, p.GetType(), p.PropertyType, p.Name, v, opt); } Utils.WriteByt(stream, '}'); stream.Flush(); return(this); }