Пример #1
0
        JObject ISavable.Save()
        {
            var result = new JObjectCollection();

            // Add the CustomName field in the JSON stream with the name CustomName
            result.Add("CustomName", CustomName);

            // As CustomInArray is an array, it must be saved as a Json Array. First we'll create one. The add method will then return it to us so we can add stuff to it
            var customIntArrayObject = result.Add("CustomIntArray", new JObjectArray());

            // Add each element in the CustomInArray to our Json Array
            foreach (var item in CustomIntArray)
            {
                customIntArrayObject.Add(item);
            }

            // Return the newly constructed
            return(result);
        }
Пример #2
0
        public JObjectCollection ParseJObjectCollection()
        {
            JObjectCollection result = new JObjectCollection();

            m_reader.Next();

            if (m_reader.Peek() == END_CURL)
            {
                m_reader.Next();
                return(result);
            }

            bool run = true;

            while (run)
            {
                string name;
                object value;

                if (m_reader.Peek() == QUOTE)
                {
                    name = ParseString();
                }
                else
                {
                    throw new JsonFormatException("Expected name", m_reader.Index(), m_reader.Line, m_reader.Context);
                }

                if (m_reader.Peek() == COLON)
                {
                    m_reader.Next();
                }
                else
                {
                    throw new JsonFormatException("Expected colon", m_reader.Index(), m_reader.Line, m_reader.Context);
                }

                value = ParseObject();

                if (m_reader.Peek() == COMMA)
                {
                    m_reader.Next();
                }
                else
                {
                    run = false;
                }

                result.Add(name, value);
            }

            m_reader.Next();

            return(result);
        }
Пример #3
0
        private JObject SerializeCustomObject(object o, HashSet <object> objects)
        {
            if (objects.Contains(o))
            {
                throw new BonaJsonException("Failed to serialize object. Cyclical data structure detected", o);
            }

            objects.Add(o);
            var result = new JObjectCollection();

            var properties = o.GetType().GetProperties();
            var fields     = o.GetType().GetFields();

            foreach (var property in properties)
            {
                if (property.PropertyType.IsArray)
                {
                    var value = property.GetValue(o, null);
                    result.Add(property.Name, SerializeArray(value, objects));
                }
                else
                {
                    result.Add(property.Name, SerializeAnyObject(property.GetValue(o, null), objects));
                }
            }
            foreach (var field in fields)
            {
                if (field.FieldType.IsArray)
                {
                    var value = field.GetValue(o);
                    result.Add(field.Name, SerializeArray(value, objects));
                }
                else
                {
                    result.Add(field.Name, SerializeAnyObject(field.GetValue(o), objects));
                }
            }


            objects.Remove(o);
            return(result);
        }
Пример #4
0
        public JObjectCollection ParseJObjectCollection()
        {
            JObjectCollection result = new JObjectCollection();
            m_reader.Next();

            if (m_reader.Peek() == END_CURL)
            {
                m_reader.Next();
                return result;
            }

            bool run = true;

            while(run)
            {
                string name;
                object value;

                if (m_reader.Peek() == QUOTE)
                {
                    name = ParseString();
                }
                else
                {
                    throw new JsonFormatException("Expected name", m_reader.Index(), m_reader.Line, m_reader.Context);
                }

                if (m_reader.Peek() == COLON)
                {
                    m_reader.Next();
                }
                else
                {
                    throw new JsonFormatException("Expected colon", m_reader.Index(), m_reader.Line, m_reader.Context);
                }

                value = ParseObject();

                if (m_reader.Peek() == COMMA)
                {
                    m_reader.Next();
                }
                else
                {
                    run = false;
                }

                result.Add(name, value);

            }

            m_reader.Next();

            return result;
        }