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();
 }
Example #2
0
 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);
 }
 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);
 }
Example #4
0
 public override void EmitNull(bool asDictionaryKey, WriteCache cache)
 {
     if (asDictionaryKey)
     {
         EmitString(Constants.EscStr, "_", "", asDictionaryKey, cache);
     }
     else
     {
         jsonWriter.WriteNull();
     }
 }
Example #5
0
 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();
        }
Example #7
0
        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();
        }
Example #8
0
 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());
     }
 }
Example #9
0
        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();
        }
Example #10
0
        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));
        }
Example #11
0
        public override void EmitInteger(object i, bool asDictionaryKey, WriteCache cache)
        {
 	        EmitInteger(Util.NumberToPrimitiveLong(i), asDictionaryKey, cache);
        }
Example #12
0
 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());
 }
Example #13
0
 public override void EmitInteger(object i, bool asDictionaryKey, WriteCache cache)
 {
     EmitInteger(Util.NumberToPrimitiveLong(i), asDictionaryKey, cache);
 }
Example #14
0
 public abstract void EmitInteger(object o, bool asDictionaryKey, WriteCache cache);
Example #15
0
 public override void EmitBinary(object b, bool asDictionaryKey, WriteCache cache)
 {
     EmitString(Constants.EscStr, "b", Convert.ToBase64String((byte[])b), asDictionaryKey, cache);
 }
Example #16
0
 public override void Emit(object obj, bool asDictionaryKey, WriteCache cache)
 {
     MarshalTop(obj, cache);
 }
Example #17
0
 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));
 }
Example #18
0
 public abstract void EmitDouble(double d, bool asDictionaryKey, WriteCache cache);
Example #19
0
 public abstract void EmitString(string prefix, string tag, string s, bool asDictionaryKey, WriteCache cache);
Example #20
0
 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));
 }
Example #21
0
 public abstract void EmitBinary(object b, bool asDictionaryKey, WriteCache cache);
Example #22
0
 public abstract void EmitDouble(double d, bool asDictionaryKey, WriteCache cache);
Example #23
0
 public abstract void EmitInteger(long i, bool asDictionaryKey, WriteCache cache);
Example #24
0
 public abstract void Emit(object obj, bool asDictionaryKey, WriteCache cache);
Example #25
0
 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);
         }
     }
 }
Example #26
0
 public abstract void EmitInteger(long i, bool asDictionaryKey, WriteCache cache);
Example #27
0
 abstract protected void EmitDictionary(IEnumerable <KeyValuePair <object, object> > keyValuePairs,
                                        bool ignored, WriteCache cache);
Example #28
0
 public abstract void EmitBinary(object b, bool asDictionaryKey, WriteCache cache);
Example #29
0
 public override void EmitDouble(double d, bool asDictionaryKey, WriteCache cache)
 {
     if (asDictionaryKey)
         EmitString(Constants.EscStr, "d", d.ToString(), asDictionaryKey, cache);
     else
         jsonWriter.WriteValue(d);
 }
Example #30
0
 public abstract void EmitNull(bool asDictionaryKey, WriteCache cache);
Example #31
0
        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();
        }
Example #32
0
        public override void EmitString(string prefix, string tag, string s, bool asDictionaryKey, WriteCache cache)
        {
            string outString = cache.CacheWrite(Util.MaybePrefix(prefix, tag, s), asDictionaryKey);

            jsonWriter.WriteValue(outString);
        }
Example #33
0
        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();
        }
Example #34
0
        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();
        }
Example #35
0
 public abstract void EmitBoolean(bool b, bool asDictionaryKey, WriteCache cache);
Example #36
0
 public override void EmitBinary(object b, bool asDictionaryKey, WriteCache cache)
 {
     EmitString(Constants.EscStr, "b", Convert.ToBase64String((byte[])b), asDictionaryKey, cache);
 }
Example #37
0
 public abstract void Emit(object obj, bool asDictionaryKey, WriteCache cache);
Example #38
0
 public override void Emit(object obj, bool asDictionaryKey, WriteCache cache)
 {
     MarshalTop(obj, cache);
 }
Example #39
0
        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());
            }
        }
Example #40
0
 abstract protected void EmitDictionary(IEnumerable<KeyValuePair<object, object>> keyValuePairs, 
     bool ignored, WriteCache cache);
Example #41
0
 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);
         }
     }
 }
Example #42
0
        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);
        }
Example #43
0
 public abstract void EmitInteger(object o, bool asDictionaryKey, WriteCache cache);
Example #44
0
 public abstract void EmitNull(bool asDictionaryKey, WriteCache cache);
Example #45
0
        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);
        }
Example #46
0
 public abstract void EmitString(string prefix, string tag, string s, bool asDictionaryKey, WriteCache cache);
Example #47
0
 public abstract void EmitBoolean(bool b, bool asDictionaryKey, WriteCache cache);
Example #48
0
 public Writer(Stream output, JsonEmitter emitter, WriteCache wc)
 {
     this.output  = output;
     this.emitter = emitter;
     this.wc      = wc;
 }
Example #49
0
 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();
 }