Beispiel #1
0
 public static JArray Parse(string json, JsonLoadSettings settings)
 {
     using (JsonReader reader = (JsonReader) new JsonTextReader((TextReader) new StringReader(json))) {
         JArray jarray = JArray.Load(reader, settings);
         do
         {
             ;
         }while (reader.Read());
         return(jarray);
     }
 }
Beispiel #2
0
 public static JObject Parse(string json, JsonLoadSettings settings)
 {
     using (JsonReader reader = (JsonReader) new JsonTextReader((TextReader) new StringReader(json)))
     {
         JObject jobject = JObject.Load(reader, settings);
         do
         {
             ;
         }while (reader.Read());
         return(jobject);
     }
 }
Beispiel #3
0
 public static JProperty Load(JsonReader reader, JsonLoadSettings settings) {
   if (reader.TokenType == JsonToken.None && !reader.Read())
     throw JsonReaderException.Create(reader, "Error reading JProperty from JsonReader.");
   reader.MoveToContent();
   if (reader.TokenType != JsonToken.PropertyName)
     throw JsonReaderException.Create(reader,
       "Error reading JProperty from JsonReader. Current JsonReader item is not a property: {0}".FormatWith(
         (IFormatProvider) CultureInfo.InvariantCulture, (object) reader.TokenType));
   JProperty jproperty = new JProperty((string) reader.Value);
   jproperty.SetLineInfo(reader as IJsonLineInfo, settings);
   jproperty.ReadTokenFrom(reader, settings);
   return jproperty;
 }
Beispiel #4
0
        public static JObject Load(JsonReader reader, JsonLoadSettings settings)
        {
            ValidationUtils.ArgumentNotNull((object)reader, nameof(reader));
            if (reader.TokenType == JsonToken.None && !reader.Read())
            {
                throw JsonReaderException.Create(reader, "Error reading JObject from JsonReader.");
            }
            reader.MoveToContent();
            if (reader.TokenType != JsonToken.StartObject)
            {
                throw JsonReaderException.Create(reader, "Error reading JObject from JsonReader. Current JsonReader item is not an object: {0}".FormatWith((IFormatProvider)CultureInfo.InvariantCulture, (object)reader.TokenType));
            }
            JObject jobject = new JObject();

            jobject.SetLineInfo(reader as IJsonLineInfo, settings);
            jobject.ReadTokenFrom(reader, settings);
            return(jobject);
        }
Beispiel #5
0
        internal void ReadTokenFrom(JsonReader reader, JsonLoadSettings options)
        {
            int depth = reader.Depth;

            if (!reader.Read())
            {
                throw JsonReaderException.Create(reader,
                                                 "Error reading {0} from JsonReader.".FormatWith((IFormatProvider)CultureInfo.InvariantCulture,
                                                                                                 (object)this.GetType().Name));
            }
            this.ReadContentFrom(reader, options);
            if (reader.Depth > depth)
            {
                throw JsonReaderException.Create(reader,
                                                 "Unexpected end of content while loading {0}.".FormatWith((IFormatProvider)CultureInfo.InvariantCulture,
                                                                                                           (object)this.GetType().Name));
            }
        }
Beispiel #6
0
        public static JArray Load(JsonReader reader, JsonLoadSettings settings)
        {
            if (reader.TokenType == JsonToken.None && !reader.Read())
            {
                throw JsonReaderException.Create(reader, "Error reading JArray from JsonReader.");
            }
            reader.MoveToContent();
            if (reader.TokenType != JsonToken.StartArray)
            {
                throw JsonReaderException.Create(reader,
                                                 "Error reading JArray from JsonReader. Current JsonReader item is not an array: {0}".FormatWith(
                                                     (IFormatProvider)CultureInfo.InvariantCulture, (object)reader.TokenType));
            }
            JArray jarray = new JArray();

            jarray.SetLineInfo(reader as IJsonLineInfo, settings);
            jarray.ReadTokenFrom(reader, settings);
            return(jarray);
        }
Beispiel #7
0
        public static JConstructor Load(JsonReader reader, JsonLoadSettings settings)
        {
            if (reader.TokenType == JsonToken.None && !reader.Read())
            {
                throw JsonReaderException.Create(reader, "Error reading JConstructor from JsonReader.");
            }
            reader.MoveToContent();
            if (reader.TokenType != JsonToken.StartConstructor)
            {
                throw JsonReaderException.Create(reader,
                                                 "Error reading JConstructor from JsonReader. Current JsonReader item is not a constructor: {0}".FormatWith(
                                                     (IFormatProvider)CultureInfo.InvariantCulture, (object)reader.TokenType));
            }
            JConstructor jconstructor = new JConstructor((string)reader.Value);

            jconstructor.SetLineInfo(reader as IJsonLineInfo, settings);
            jconstructor.ReadTokenFrom(reader, settings);
            return(jconstructor);
        }
Beispiel #8
0
        internal void ReadContentFrom(JsonReader r, JsonLoadSettings settings)
        {
            ValidationUtils.ArgumentNotNull((object)r, nameof(r));
            IJsonLineInfo lineInfo   = r as IJsonLineInfo;
            JContainer    jcontainer = this;

            do
            {
                if ((jcontainer as JProperty)?.Value != null)
                {
                    if (jcontainer == this)
                    {
                        break;
                    }
                    jcontainer = jcontainer.Parent;
                }

                switch (r.TokenType)
                {
                case JsonToken.None:
                    continue;

                case JsonToken.StartObject:
                    JObject jobject = new JObject();
                    jobject.SetLineInfo(lineInfo, settings);
                    jcontainer.Add((object)jobject);
                    jcontainer = (JContainer)jobject;
                    goto case JsonToken.None;

                case JsonToken.StartArray:
                    JArray jarray = new JArray();
                    jarray.SetLineInfo(lineInfo, settings);
                    jcontainer.Add((object)jarray);
                    jcontainer = (JContainer)jarray;
                    goto case JsonToken.None;

                case JsonToken.StartConstructor:
                    JConstructor jconstructor = new JConstructor(r.Value.ToString());
                    jconstructor.SetLineInfo(lineInfo, settings);
                    jcontainer.Add((object)jconstructor);
                    jcontainer = (JContainer)jconstructor;
                    goto case JsonToken.None;

                case JsonToken.PropertyName:
                    string    name       = r.Value.ToString();
                    JProperty jproperty1 = new JProperty(name);
                    jproperty1.SetLineInfo(lineInfo, settings);
                    JProperty jproperty2 = ((JObject)jcontainer).Property(name);
                    if (jproperty2 == null)
                    {
                        jcontainer.Add((object)jproperty1);
                    }
                    else
                    {
                        jproperty2.Replace((JToken)jproperty1);
                    }
                    jcontainer = (JContainer)jproperty1;
                    goto case JsonToken.None;

                case JsonToken.Comment:
                    if (settings != null && settings.CommentHandling == CommentHandling.Load)
                    {
                        JValue comment = JValue.CreateComment(r.Value.ToString());
                        comment.SetLineInfo(lineInfo, settings);
                        jcontainer.Add((object)comment);
                        goto case JsonToken.None;
                    }
                    else
                    {
                        goto case JsonToken.None;
                    }

                case JsonToken.Integer:
                case JsonToken.Float:
                case JsonToken.String:
                case JsonToken.Boolean:
                case JsonToken.Date:
                case JsonToken.Bytes:
                    JValue jvalue1 = new JValue(r.Value);
                    jvalue1.SetLineInfo(lineInfo, settings);
                    jcontainer.Add((object)jvalue1);
                    goto case JsonToken.None;

                case JsonToken.Null:
                    JValue jvalue2 = JValue.CreateNull();
                    jvalue2.SetLineInfo(lineInfo, settings);
                    jcontainer.Add((object)jvalue2);
                    goto case JsonToken.None;

                case JsonToken.Undefined:
                    JValue undefined = JValue.CreateUndefined();
                    undefined.SetLineInfo(lineInfo, settings);
                    jcontainer.Add((object)undefined);
                    goto case JsonToken.None;

                case JsonToken.EndObject:
                    if (jcontainer == this)
                    {
                        return;
                    }
                    jcontainer = jcontainer.Parent;
                    goto case JsonToken.None;

                case JsonToken.EndArray:
                    if (jcontainer == this)
                    {
                        return;
                    }
                    jcontainer = jcontainer.Parent;
                    goto case JsonToken.None;

                case JsonToken.EndConstructor:
                    if (jcontainer == this)
                    {
                        return;
                    }
                    jcontainer = jcontainer.Parent;
                    goto case JsonToken.None;

                default:
                    throw new InvalidOperationException(
                              "The JsonReader should not be on a token of type {0}.".FormatWith(
                                  (IFormatProvider)CultureInfo.InvariantCulture, (object)r.TokenType));
                }
            } while (r.Read());
        }