protected override void EmitTagged(string t, object obj, bool ignored, WriteCache cache) { EmitDictionaryStart(1L); EmitString(Constants.EscTag, t, "", true, cache); Marshal(obj, false, cache); EmitDictionaryEnd(); }
public override void EmitString(string prefix, string tag, string s, bool asDictionaryKey, WriteCache cache) { string outString = cache.CacheWrite(Util.MaybePrefix(prefix, tag, s), asDictionaryKey); if (asDictionaryKey) jsonWriter.WritePropertyName(outString); else jsonWriter.WriteValue(outString); }
public override void EmitBoolean(bool b, bool asDictionaryKey, WriteCache cache) { if (asDictionaryKey) { EmitString(Constants.EscStr, "?", b ? "t" : "f", asDictionaryKey, cache); } else { jsonWriter.WriteValue(b); } }
protected override void EmitDictionary(IEnumerable<KeyValuePair<object, object>> keyValuePairs, bool ignored, WriteCache cache) { long sz = keyValuePairs.Count(); EmitDictionaryStart(sz); foreach (KeyValuePair<object, object> item in keyValuePairs) { Marshal(item.Key, true, cache); Marshal(item.Value, false, cache); } EmitDictionaryEnd(); }
protected override void EmitDictionary(IEnumerable <KeyValuePair <object, object> > keyValuePairs, bool ignored, WriteCache cache) { long sz = keyValuePairs.Count(); EmitDictionaryStart(sz); foreach (KeyValuePair <object, object> item in keyValuePairs) { Marshal(item.Key, true, cache); Marshal(item.Value, false, cache); } EmitDictionaryEnd(); }
public override void EmitDouble(object d, bool asDictionaryKey, WriteCache cache) { if (d is double) { EmitDouble((double)d, asDictionaryKey, cache); } else if (d is float) { EmitDouble((float)d, asDictionaryKey, cache); } else { throw new TransitException("Unknown double type: " + d.GetType()); } }
protected override void EmitDictionary(IEnumerable <KeyValuePair <object, object> > keyValuePairs, bool ignored, WriteCache cache) { long sz = Enumerable.Count(keyValuePairs); EmitListStart(sz); EmitString(null, null, Constants.DirectoryAsList, false, cache); foreach (var kvp in keyValuePairs) { Marshal(kvp.Key, true, cache); Marshal(kvp.Value, false, cache); } EmitListEnd(); }
public static IWriter <T> GetJsonInstance <T>(Stream output, IDictionary <Type, IWriteHandler> customHandlers, bool verboseMode) { TextWriter textWriter = new StreamWriter(output); JsonWriter jsonWriter = new JsonTextWriter(textWriter); IImmutableDictionary <Type, IWriteHandler> handlers = Handlers(customHandlers); JsonEmitter emitter; if (verboseMode) { emitter = new JsonVerboseEmitter(jsonWriter, GetVerboseHandlers(handlers)); } else { emitter = new JsonEmitter(jsonWriter, handlers); } SetSubHandler(handlers, emitter); WriteCache wc = new WriteCache(!verboseMode); return(new Writer <T>(output, emitter, wc)); }
protected void MarshalTop(object obj, WriteCache cache) { IWriteHandler handler = GetHandler(obj); if (handler == null) { throw new NotSupportedException( string.Format("Cannot marshal type {0} ({1})", obj != null ? obj.GetType() : null, obj)); } string tag = handler.Tag(obj); if (tag == null) { throw new NotSupportedException( string.Format("Cannot marshal type {0} ({1})", obj != null ? obj.GetType() : null, obj)); } if (tag.Length == 1) obj = new Quote(obj); Marshal(obj, false, cache); }
protected virtual void EmitTagged(string t, object obj, bool ignored, WriteCache cache) { EmitListStart(2L); EmitString(Constants.EscTag, t, "", false, cache); Marshal(obj, false, cache); EmitListEnd(); }
protected void EmitList(object o, bool ignored, WriteCache cache) { var enumerable = o as IEnumerable; var length = enumerable.Cast <object>().Count(); EmitListStart(length); if (o is IEnumerable <int> ) { foreach (var n in (IEnumerable <int>)o) { EmitInteger(n, false, cache); } } else if (o is IEnumerable <short> ) { foreach (var n in (IEnumerable <short>)o) { EmitInteger(n, false, cache); } } else if (o is IEnumerable <long> ) { foreach (var n in (IEnumerable <long>)o) { EmitInteger(n, false, cache); } } else if (o is IEnumerable <float> ) { foreach (var n in (IEnumerable <float>)o) { EmitDouble(n, false, cache); } } else if (o is IEnumerable <double> ) { foreach (var n in (IEnumerable <double>)o) { EmitDouble(n, false, cache); } } else if (o is IEnumerable <bool> ) { foreach (var n in (IEnumerable <bool>)o) { EmitBoolean(n, false, cache); } } else if (o is IEnumerable <char> ) { foreach (var n in (IEnumerable <char>)o) { Marshal(n, false, cache); } } else { foreach (var n in enumerable) { Marshal(n, false, cache); } } EmitListEnd(); }
public abstract void EmitNull(bool asDictionaryKey, WriteCache cache);
public abstract void EmitString(string prefix, string tag, string s, bool asDictionaryKey, WriteCache cache);
public override void Emit(object obj, bool asDictionaryKey, WriteCache cache) { MarshalTop(obj, cache); }
public override void EmitString(string prefix, string tag, string s, bool asDictionaryKey, WriteCache cache) { string outString = cache.CacheWrite(Util.MaybePrefix(prefix, tag, s), asDictionaryKey); if (asDictionaryKey) { jsonWriter.WritePropertyName(outString); } else { jsonWriter.WriteValue(outString); } }
public void TestWriteCacheDisabled() { WriteCache wc = new WriteCache(false); Assert.AreEqual("foobar", wc.CacheWrite("foobar", false)); Assert.AreEqual("foobar", wc.CacheWrite("foobar", false)); Assert.AreEqual("foobar", wc.CacheWrite("foobar", true)); Assert.AreEqual("foobar", wc.CacheWrite("foobar", true)); }
public void TestWriteCache() { WriteCache wc = new WriteCache(true); Assert.AreEqual("~:foo", wc.CacheWrite("~:foo", false)); Assert.AreEqual("^" + (char)WriteCache.BaseCharIdx, wc.CacheWrite("~:foo", false)); Assert.AreEqual("~$bar", wc.CacheWrite("~$bar", false)); Assert.AreEqual("^" + (char)(WriteCache.BaseCharIdx + 1), wc.CacheWrite("~$bar", false)); Assert.AreEqual("~#baz", wc.CacheWrite("~#baz", false)); Assert.AreEqual("^" + (char)(WriteCache.BaseCharIdx + 2), wc.CacheWrite("~#baz", false)); Assert.AreEqual("foobar", wc.CacheWrite("foobar", false)); Assert.AreEqual("foobar", wc.CacheWrite("foobar", false)); Assert.AreEqual("foobar", wc.CacheWrite("foobar", true)); Assert.AreEqual("^" + (char)(WriteCache.BaseCharIdx + 3), wc.CacheWrite("foobar", true)); Assert.AreEqual("abc", wc.CacheWrite("abc", false)); Assert.AreEqual("abc", wc.CacheWrite("abc", false)); Assert.AreEqual("abc", wc.CacheWrite("abc", true)); Assert.AreEqual("abc", wc.CacheWrite("abc", true)); }
public abstract void EmitBinary(object b, bool asDictionaryKey, WriteCache cache);
public abstract void EmitDouble(double d, bool asDictionaryKey, WriteCache cache);
public abstract void EmitInteger(long i, bool asDictionaryKey, WriteCache cache);
public abstract void Emit(object obj, bool asDictionaryKey, WriteCache cache);
public abstract void EmitInteger(object o, bool asDictionaryKey, WriteCache cache);
public Writer(Stream output, JsonEmitter emitter, WriteCache wc) { this.output = output; this.emitter = emitter; this.wc = wc; }
abstract protected void EmitDictionary(IEnumerable <KeyValuePair <object, object> > keyValuePairs, bool ignored, WriteCache cache);
public override void EmitBinary(object b, bool asDictionaryKey, WriteCache cache) { EmitString(Constants.EscStr, "b", Convert.ToBase64String((byte[])b), asDictionaryKey, cache); }
public override void EmitDouble(object d, bool asDictionaryKey, WriteCache cache) { if (d is double) EmitDouble((double)d, asDictionaryKey, cache); else if (d is float) EmitDouble((float)d, asDictionaryKey, cache); else throw new TransitException("Unknown double type: " + d.GetType()); }
public override void EmitInteger(object i, bool asDictionaryKey, WriteCache cache) { EmitInteger(Util.NumberToPrimitiveLong(i), asDictionaryKey, cache); }
public abstract void EmitBoolean(bool b, bool asDictionaryKey, WriteCache cache);
protected void EmitList(object o, bool ignored, WriteCache cache) { var enumerable = o as IEnumerable; var length = enumerable.Cast<object>().Count(); EmitListStart(length); if (o is IEnumerable<int>) { foreach (var n in (IEnumerable<int>)o) { EmitInteger(n, false, cache); } } else if (o is IEnumerable<short>) { foreach (var n in (IEnumerable<short>)o) { EmitInteger(n, false, cache); } } else if (o is IEnumerable<long>) { foreach (var n in (IEnumerable<long>)o) { EmitInteger(n, false, cache); } } else if (o is IEnumerable<float>) { foreach (var n in (IEnumerable<float>)o) { EmitDouble(n, false, cache); } } else if (o is IEnumerable<double>) { foreach (var n in (IEnumerable<double>)o) { EmitDouble(n, false, cache); } } else if (o is IEnumerable<bool>) { foreach (var n in (IEnumerable<bool>)o) { EmitBoolean(n, false, cache); } } else if (o is IEnumerable<char>) { foreach (var n in (IEnumerable<char>)o) { Marshal(n, false, cache); } } else { foreach (var n in enumerable) { Marshal(n, false, cache); } } EmitListEnd(); }
protected void Marshal(object o, bool asDictionaryKey, WriteCache cache) { bool supported = false; IWriteHandler h = GetHandler(o); if (h != null) { string t = h.Tag(o); if (t != null) { supported = true; if(t.Length == 1) { switch (t[0]) { case '_': EmitNull(asDictionaryKey, cache); break; case 's': EmitString(null, null, Escape((string)h.Representation(o)), asDictionaryKey, cache); break; case '?': EmitBoolean((bool)h.Representation(o), asDictionaryKey, cache); break; case 'i': EmitInteger(h.Representation(o), asDictionaryKey, cache); break; case 'd': EmitDouble(h.Representation(o), asDictionaryKey, cache); break; case 'b': EmitBinary(h.Representation(o), asDictionaryKey, cache); break; case '\'': EmitTagged(t, h.Representation(o), false, cache); break; default: EmitEncoded(t, h, o, asDictionaryKey, cache); break; } } else { if (t.Equals("array")) { EmitList(h.Representation(o), asDictionaryKey, cache); } else if (t.Equals("map")) { EmitDictionary(h.Representation(o), asDictionaryKey, cache); } else { EmitEncoded(t, h, o, asDictionaryKey, cache); } } FlushWriter(); } } if (!supported) { throw new NotSupportedException("Not supported: " + o.GetType()); } }
private void EmitDictionary(dynamic keyValuePairEnumerable, bool ignored, WriteCache cache) { var d = new Dictionary<object, object>(); foreach (var item in keyValuePairEnumerable) { d.Add(item.Key, item.Value); } EmitDictionary(d, ignored, cache); }
protected abstract void EmitDictionary(IEnumerable<KeyValuePair<object, object>> keyValuePairs, bool ignored, WriteCache cache);
public override void EmitDouble(double d, bool asDictionaryKey, WriteCache cache) { if (asDictionaryKey) EmitString(Constants.EscStr, "d", d.ToString(), asDictionaryKey, cache); else jsonWriter.WriteValue(d); }
public override void EmitInteger(long i, bool asDictionaryKey, WriteCache cache) { if (asDictionaryKey || i > JsonIntMax || i < JsonIntMin) EmitString(Constants.EscStr, "i", i.ToString(), asDictionaryKey, cache); else jsonWriter.WriteValue(i); }
protected void EmitEncoded(string t, IWriteHandler handler, object obj, bool asDictionaryKey, WriteCache cache) { if (t.Length == 1) { object r = handler.Representation(obj); if (r is string) { EmitString(Constants.EscStr, t, (string)r, asDictionaryKey, cache); } else if (PrefersStrings() || asDictionaryKey) { string sr = handler.StringRepresentation(obj); if (sr != null) { EmitString(Constants.EscStr, t, sr, asDictionaryKey, cache); } else { throw new TransitException("Cannot be encoded as a string " + obj); } } else { EmitTagged(t, r, asDictionaryKey, cache); } } else { if (asDictionaryKey) { throw new TransitException("Cannot be used as a map key " + obj); } else { EmitTagged(t, handler.Representation(obj), asDictionaryKey, cache); } } }
public override void EmitNull(bool asDictionaryKey, WriteCache cache) { if (asDictionaryKey) { EmitString(Constants.EscStr, "_", "", asDictionaryKey, cache); } else { jsonWriter.WriteNull(); } }
protected override void EmitDictionary(IEnumerable<KeyValuePair<object, object>> keyValuePairs, bool ignored, WriteCache cache) { long sz = Enumerable.Count(keyValuePairs); EmitListStart(sz); EmitString(null, null, Constants.DirectoryAsList, false, cache); foreach (var kvp in keyValuePairs) { Marshal(kvp.Key, true, cache); Marshal(kvp.Value, false, cache); } EmitListEnd(); }