예제 #1
0
        private static JsonReader Export(object value)
        {
            var writer = new JsonBufferWriter();

            JsonConvert.Export(value, writer);
            return(writer.GetBuffer().CreateReader());
        }
예제 #2
0
 static StockJsonBuffers()
 {
     JsonBufferWriter writer = new JsonBufferWriter(2);
     writer.WriteStartObject();
     writer.WriteEndObject();
     EmptyObject = writer.GetBuffer();
 }
예제 #3
0
        static StockJsonBuffers()
        {
            JsonBufferWriter writer = new JsonBufferWriter(2);

            writer.WriteStartObject();
            writer.WriteEndObject();
            EmptyObject = writer.GetBuffer();
        }
예제 #4
0
        public void SubClassCannotExportValueWithNullWriter()
        {
            var tuple    = Tuple.Create(42);
            var exporter = new ExportValueTestExporter(tuple.GetType());

            exporter.ForceNullWriter = true;
            var context = JsonConvert.CreateExportContext();
            var writer  = new JsonBufferWriter();

            exporter.Export(context, tuple, writer);
        }
        public virtual ObjectConstructionResult CreateObject(ImportContext context)
        {
            if (context == null) throw new ArgumentNullException("context");

            JsonBufferWriter tailw = _tailw;
            _tailw = null;
            if (tailw != null)
                tailw.WriteEndObject();
            JsonBuffer tail = (tailw != null ? tailw.GetBuffer() : StockJsonBuffers.EmptyObject);
            object obj = OnCreateObject(context);
            return new ObjectConstructionResult(obj, tail.CreateReader());
        }
        public virtual bool Import(ImportContext context, string name, JsonReader reader)
        {
            if (context == null) throw new ArgumentNullException("context");
            if (reader == null) throw new ArgumentNullException("reader");

            JsonBufferWriter tailw = _tailw;
            if (tailw == null)
            {
                tailw = _tailw = new JsonBufferWriter();
                tailw.WriteStartObject();
            }
            tailw.WriteMember(name);
            tailw.WriteFromReader(reader);
            return true;
        }
예제 #7
0
        public virtual ObjectConstructionResult CreateObject(ImportContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var tailw = _tailw;

            _tailw = null;
            tailw?.WriteEndObject();
            var tail = tailw?.GetBuffer() ?? StockJsonBuffers.EmptyObject;
            var obj  = OnCreateObject(context);

            return(new ObjectConstructionResult(obj, tail.CreateReader()));
        }
예제 #8
0
        public virtual ObjectConstructionResult CreateObject(ImportContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            JsonBufferWriter tailw = _tailw;

            _tailw = null;
            if (tailw != null)
            {
                tailw.WriteEndObject();
            }
            JsonBuffer tail = (tailw != null ? tailw.GetBuffer() : StockJsonBuffers.EmptyObject);
            object     obj  = OnCreateObject(context);

            return(new ObjectConstructionResult(obj, tail.CreateReader()));
        }
예제 #9
0
        public virtual bool Import(ImportContext context, string name, JsonReader reader)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }

            JsonBufferWriter tailw = _tailw;

            if (tailw == null)
            {
                tailw = _tailw = new JsonBufferWriter();
                tailw.WriteStartObject();
            }
            tailw.WriteMember(name);
            tailw.WriteFromReader(reader);
            return(true);
        }
예제 #10
0
 public void SubClassCannotExportValueWithNullWriter()
 {
     var tuple = Tuple.Create(42);
     var exporter = new ExportValueTestExporter(tuple.GetType());
     exporter.ForceNullWriter = true;
     var context = JsonConvert.CreateExportContext();
     var writer = new JsonBufferWriter();
     exporter.Export(context, tuple, writer);
 }
예제 #11
0
 private static JsonReader Export(object value)
 {
     var writer = new JsonBufferWriter();
     JsonConvert.Export(value, writer);
     return writer.GetBuffer().CreateReader();
 }
예제 #12
0
        private static ObjectConstructionResult TryCreateObject(ImportContext context, ConstructorInfo ctor, NamedJsonBuffer[] members)
        {
            Debug.Assert(context != null);
            Debug.Assert(ctor != null);
            Debug.Assert(members != null);

            ParameterInfo[] parameters = ctor.GetParameters();

            if (parameters.Length > members.Length)
            {
                return(null);
            }

            int[] bindings = Bind(context, parameters, members);

            int argc = 0;

            object[]         args  = null;
            JsonBufferWriter tailw = null;

            for (int i = 0; i < bindings.Length; i++)
            {
                int binding = bindings[i] - 1;

                if (binding >= 0)
                {
                    if (args == null)
                    {
                        args = new object[parameters.Length];
                    }

                    Type       type = parameters[binding].ParameterType;
                    JsonBuffer arg  = members[i].Buffer;
                    args[binding] = context.Import(type, arg.CreateReader());
                    argc++;
                }
                else
                {
                    if (tailw == null)
                    {
                        tailw = new JsonBufferWriter();
                        tailw.WriteStartObject();
                    }

                    NamedJsonBuffer member = members[i];
                    tailw.WriteMember(member.Name);
                    tailw.WriteFromReader(member.Buffer.CreateReader());
                }
            }

            if (tailw != null)
            {
                tailw.WriteEndObject();
            }

            if (argc != parameters.Length)
            {
                return(null);
            }

            object obj = ctor.Invoke(args);

            JsonBuffer tail = tailw != null
                            ? tailw.GetBuffer()
                            : StockJsonBuffers.EmptyObject;

            return(new ObjectConstructionResult(obj, tail.CreateReader()));
        }
예제 #13
0
        private static ObjectConstructionResult TryCreateObject(ImportContext context, ConstructorInfo ctor, NamedJsonBuffer[] members)
        {
            Debug.Assert(context != null);
            Debug.Assert(ctor != null);
            Debug.Assert(members != null);

            ParameterInfo[] parameters = ctor.GetParameters();
            
            if (parameters.Length > members.Length)
                return null;

            int[] bindings = Bind(context, parameters, members);

            int argc = 0;
            object[] args = null;
            JsonBufferWriter tailw = null;

            for (int i = 0; i < bindings.Length; i++)
            {
                int binding = bindings[i] - 1;
                
                if (binding >= 0)
                {
                    if (args == null)
                        args = new object[parameters.Length];

                    Type type = parameters[binding].ParameterType;
                    JsonBuffer arg = members[i].Buffer;
                    args[binding] = context.Import(type, arg.CreateReader());
                    argc++;
                }
                else
                {
                    if (tailw == null)
                    {
                        tailw = new JsonBufferWriter();
                        tailw.WriteStartObject();
                    }

                    NamedJsonBuffer member = members[i];
                    tailw.WriteMember(member.Name);
                    tailw.WriteFromReader(member.Buffer.CreateReader());
                }
            }

            if (tailw != null)
                tailw.WriteEndObject();

            if (argc != parameters.Length) 
                return null;

            object obj = ctor.Invoke(args);

            JsonBuffer tail = tailw != null 
                            ? tailw.GetBuffer() 
                            : StockJsonBuffers.EmptyObject;
            
            return new ObjectConstructionResult(obj, tail.CreateReader());
        }