示例#1
0
        /// <summary>
        /// Asynchronously loads a <see cref="JConstructor"/> from a <see cref="JsonReader"/>.
        /// </summary>
        /// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JConstructor"/>.</param>
        /// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
        /// If this is <c>null</c>, default load settings will be used.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
        /// <returns>
        /// A <see cref="Task{TResult}"/> that represents the asynchronous load. The <see cref="Task{TResult}.Result"/>
        /// property returns a <see cref="JConstructor"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
        public new static async Task <JConstructor> LoadAsync(JsonReader reader, JsonLoadSettings settings, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (reader.TokenType == JsonToken.None)
            {
                if (!await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
                {
                    throw JsonReaderException.Create(reader, "Error reading JConstructor from JsonReader.");
                }
            }

            await reader.MoveToContentAsync(cancellationToken).ConfigureAwait(false);

            if (reader.TokenType != JsonToken.StartConstructor)
            {
                throw JsonReaderException.Create(reader, "Error reading JConstructor from JsonReader. Current JsonReader item is not a constructor: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
            }

            JConstructor c = new JConstructor((string)reader.Value);

            c.SetLineInfo(reader as IJsonLineInfo, settings);

            await c.ReadTokenFromAsync(reader, settings, cancellationToken).ConfigureAwait(false);

            return(c);
        }
示例#2
0
        /// <summary>
        /// Loads an <see cref="JArray"/> from a <see cref="JsonReader"/>.
        /// </summary>
        /// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JArray"/>.</param>
        /// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
        /// If this is <c>null</c>, default load settings will be used.</param>
        /// <returns>A <see cref="JArray"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
        public new static JArray Load(JsonReader reader, JsonLoadSettings settings)
        {
            if (reader.TokenType == JsonToken.None)
            {
                if (!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(CultureInfo.InvariantCulture, reader.TokenType));
            }

            JArray a = new JArray();

            a.SetLineInfo(reader as IJsonLineInfo, settings);

            a.ReadTokenFrom(reader, settings);

            return(a);
        }
示例#3
0
        /// <summary>
        /// Asynchronously loads a <see cref="JProperty"/> from a <see cref="JsonReader"/>.
        /// </summary>
        /// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JProperty"/>.</param>
        /// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
        /// If this is <c>null</c>, default load settings will be used.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
        /// <returns>A <see cref="Task{TResult}"/> representing the asynchronous creation. The <see cref="Task{TResult}.Result"/>
        /// property returns a <see cref="JProperty"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
        public new static async Task <JProperty> LoadAsync(JsonReader reader, JsonLoadSettings settings, CancellationToken cancellationToken = default)
        {
            if (reader.TokenType == JsonToken.None)
            {
                if (!await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
                {
                    throw JsonReaderException.Create(reader, "Error reading JProperty from JsonReader.");
                }
            }

            await reader.MoveToContentAsync(cancellationToken).ConfigureAwait(false);

            if (reader.TokenType != JsonToken.PropertyName)
            {
                throw JsonReaderException.Create(reader, "Error reading JProperty from JsonReader. Current JsonReader item is not a property: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
            }

            JProperty p = new JProperty((string)reader.Value);

            p.SetLineInfo(reader as IJsonLineInfo, settings);

            await p.ReadTokenFromAsync(reader, settings, cancellationToken).ConfigureAwait(false);

            return(p);
        }
示例#4
0
        /// <summary>
        /// Loads an <see cref="JProperty"/> from a <see cref="JsonReader"/>.
        /// </summary>
        /// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JProperty"/>.</param>
        /// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
        /// If this is null, default load settings will be used.</param>
        /// <returns>A <see cref="JProperty"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
        public new static JProperty Load(JsonReader reader, JsonLoadSettings settings)
        {
            if (reader.TokenType == JsonToken.None)
            {
                if (!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(CultureInfo.InvariantCulture, reader.TokenType));
            }

            JProperty p = new JProperty((string)reader.Value);

            p.SetLineInfo(reader as IJsonLineInfo, settings);

            p.ReadTokenFrom(reader, settings);

            return(p);
        }
示例#5
0
        /// <summary>
        /// Loads a <see cref="JObject"/> from a <see cref="JsonReader"/>.
        /// </summary>
        /// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JObject"/>.</param>
        /// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
        /// If this is <c>null</c>, default load settings will be used.</param>
        /// <returns>A <see cref="JObject"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
        /// <exception cref="JsonReaderException">
        ///     <paramref name="reader"/> is not valid JSON.
        /// </exception>
        public new static JObject Load(JsonReader reader, JsonLoadSettings settings)
        {
            ValidationUtils.ArgumentNotNull(reader, nameof(reader));

            if (reader.TokenType == JsonToken.None)
            {
                if (!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(CultureInfo.InvariantCulture, reader.TokenType));
            }

            JObject o = new JObject();

            o.SetLineInfo(reader as IJsonLineInfo, settings);

            o.ReadTokenFrom(reader, settings);

            return(o);
        }
示例#6
0
        /// <summary>
        /// Loads an <see cref="JConstructor"/> from a <see cref="JsonReader"/>.
        /// </summary>
        /// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JConstructor"/>.</param>
        /// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
        /// If this is null, default load settings will be used.</param>
        /// <returns>A <see cref="JConstructor"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
        public new static JConstructor Load(JsonReader reader, JsonLoadSettings settings)
        {
            if (reader.TokenType == JsonToken.None)
            {
                if (!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(CultureInfo.InvariantCulture, reader.TokenType));
            }

            JConstructor c = new JConstructor((string)reader.Value);

            c.SetLineInfo(reader as IJsonLineInfo, settings);

            c.ReadTokenFrom(reader, settings);

            return(c);
        }
示例#7
0
        /// <summary>
        /// Asynchronously loads a <see cref="JArray"/> from a <see cref="JsonReader"/>.
        /// </summary>
        /// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JArray"/>.</param>
        /// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
        /// If this is <c>null</c>, default load settings will be used.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
        /// <returns>A <see cref="Task{TResult}"/> representing the asynchronous load. The <see cref="Task{TResult}.Result"/> property contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
        public new static async Task <JArray> LoadAsync(JsonReader reader, JsonLoadSettings settings, CancellationToken cancellationToken = default)
        {
            if (reader.TokenType == JsonToken.None)
            {
                if (!await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
                {
                    throw JsonReaderException.Create(reader, "Error reading JArray from JsonReader.");
                }
            }

            await reader.MoveToContentAsync(cancellationToken).ConfigureAwait(false);

            if (reader.TokenType != JsonToken.StartArray)
            {
                throw JsonReaderException.Create(reader, "Error reading JArray from JsonReader. Current JsonReader item is not an array: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
            }

            JArray a = new JArray();

            a.SetLineInfo(reader as IJsonLineInfo, settings);

            await a.ReadTokenFromAsync(reader, settings, cancellationToken).ConfigureAwait(false);

            return(a);
        }
示例#8
0
        /// <summary>
        /// Asynchronously creates a <see cref="JToken"/> from a <see cref="JsonReader"/>.
        /// </summary>
        /// <param name="reader">An <see cref="JsonReader"/> positioned at the token to read into this <see cref="JToken"/>.</param>
        /// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
        /// If this is <c>null</c>, default load settings will be used.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
        /// <returns>
        /// A <see cref="Task{TResult}"/> that represents the asynchronous creation. The
        /// <see cref="Task{TResult}.Result"/> property returns a <see cref="JToken"/> that contains
        /// the token and its descendant tokens
        /// that were read from the reader. The runtime type of the token is determined
        /// by the token type of the first token encountered in the reader.
        /// </returns>
        public static async Task <JToken> ReadFromAsync(JsonReader reader, JsonLoadSettings settings, CancellationToken cancellationToken = default(CancellationToken))
        {
            ValidationUtils.ArgumentNotNull(reader, nameof(reader));

            if (reader.TokenType == JsonToken.None)
            {
                if (!await(settings != null && settings.CommentHandling == CommentHandling.Ignore ? reader.ReadAndMoveToContentAsync(cancellationToken) : reader.ReadAsync(cancellationToken)).ConfigureAwait(false))
                {
                    throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader.");
                }
            }

            IJsonLineInfo lineInfo = reader as IJsonLineInfo;

            switch (reader.TokenType)
            {
            case JsonToken.StartObject:
                return(await JObject.LoadAsync(reader, settings, cancellationToken).ConfigureAwait(false));

            case JsonToken.StartArray:
                return(await JArray.LoadAsync(reader, settings, cancellationToken).ConfigureAwait(false));

            case JsonToken.StartConstructor:
                return(await JConstructor.LoadAsync(reader, settings, cancellationToken).ConfigureAwait(false));

            case JsonToken.PropertyName:
                return(await JProperty.LoadAsync(reader, settings, cancellationToken).ConfigureAwait(false));

            case JsonToken.String:
            case JsonToken.Integer:
            case JsonToken.Float:
            case JsonToken.Date:
            case JsonToken.Boolean:
            case JsonToken.Bytes:
                JValue v = new JValue(reader.Value);
                v.SetLineInfo(lineInfo, settings);
                return(v);

            case JsonToken.Comment:
                v = JValue.CreateComment(reader.Value.ToString());
                v.SetLineInfo(lineInfo, settings);
                return(v);

            case JsonToken.Null:
                v = JValue.CreateNull();
                v.SetLineInfo(lineInfo, settings);
                return(v);

            case JsonToken.Undefined:
                v = JValue.CreateUndefined();
                v.SetLineInfo(lineInfo, settings);
                return(v);

            default:
                throw JsonReaderException.Create(reader, "Error reading JToken from JsonReader. Unexpected token: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
            }
        }
示例#9
0
 public static JArray Parse(string json, JsonLoadSettings settings)
 {
     using (JsonReader reader = new JsonTextReader(new StringReader(json)))
     {
         if (reader.Read() && (reader.TokenType != JsonToken.Comment))
         {
             throw JsonReaderException.Create(reader, "Additional text found in JSON string after parsing content.");
         }
         return(Load(reader, settings));
     }
 }
示例#10
0
        // Token: 0x06001268 RID: 4712 RVA: 0x00064830 File Offset: 0x00062A30
        internal void ReadTokenFrom(JsonReader reader, [Nullable(2)] JsonLoadSettings options)
        {
            int depth = reader.Depth;

            if (!reader.Read())
            {
                throw JsonReaderException.Create(reader, "Error reading {0} from JsonReader.".FormatWith(CultureInfo.InvariantCulture, base.GetType().Name));
            }
            this.ReadContentFrom(reader, options);
            if (reader.Depth > depth)
            {
                throw JsonReaderException.Create(reader, "Unexpected end of content while loading {0}.".FormatWith(CultureInfo.InvariantCulture, base.GetType().Name));
            }
        }
示例#11
0
文件: JArray.cs 项目: zha0/Cerberus
        // Token: 0x06001210 RID: 4624 RVA: 0x0006383C File Offset: 0x00061A3C
        public new static JArray Parse(string json, [Nullable(2)] JsonLoadSettings settings)
        {
            JArray result;

            using (JsonReader jsonReader = new JsonTextReader(new StringReader(json)))
            {
                JArray jarray = JArray.Load(jsonReader, settings);
                while (jsonReader.Read())
                {
                }
                result = jarray;
            }
            return(result);
        }
示例#12
0
文件: JObject.cs 项目: zha0/Cerberus
        // Token: 0x060012BF RID: 4799 RVA: 0x000657D8 File Offset: 0x000639D8
        public new static JObject Parse(string json, [Nullable(2)] JsonLoadSettings settings)
        {
            JObject result;

            using (JsonReader jsonReader = new JsonTextReader(new StringReader(json)))
            {
                JObject jobject = JObject.Load(jsonReader, settings);
                while (jsonReader.Read())
                {
                }
                result = jobject;
            }
            return(result);
        }
示例#13
0
        public static new JArray Parse(string json, JsonLoadSettings settings)
        {
            JArray jArrays;

            using (JsonReader jsonTextReader = new JsonTextReader(new StringReader(json)))
            {
                JArray jArrays1 = JArray.Load(jsonTextReader, settings);
                while (jsonTextReader.Read())
                {
                }
                jArrays = jArrays1;
            }
            return(jArrays);
        }
示例#14
0
        /// <summary>
        /// Load a <see cref="JArray"/> from a string that contains JSON.
        /// </summary>
        /// <param name="json">A <see cref="String"/> that contains JSON.</param>
        /// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
        /// If this is <c>null</c>, default load settings will be used.</param>
        /// <returns>A <see cref="JArray"/> populated from the string that contains JSON.</returns>
        /// <example>
        ///   <code lang="cs" source="..\Src\Newtonsoft.Json.Tests\Documentation\LinqToJsonTests.cs" region="LinqToJsonCreateParseArray" title="Parsing a JSON Array from Text" />
        /// </example>
        public new static JArray Parse(string json, JsonLoadSettings settings)
        {
            using (JsonReader reader = new JsonTextReader(new StringReader(json)))
            {
                JArray a = Load(reader, settings);

                while (reader.Read())
                {
                    // Any content encountered here other than a comment will throw in the reader.
                }

                return(a);
            }
        }
示例#15
0
        public static new JArray Parse(string json, JsonLoadSettings settings)
        {
            JArray jArrays;

            using (JsonReader jsonTextReader = new JsonTextReader(new StringReader(json)))
            {
                JArray jArrays1 = JArray.Load(jsonTextReader, settings);
                if (jsonTextReader.Read() && jsonTextReader.TokenType != JsonToken.Comment)
                {
                    throw JsonReaderException.Create(jsonTextReader, "Additional text found in JSON string after parsing content.");
                }
                jArrays = jArrays1;
            }
            return(jArrays);
        }
示例#16
0
        /// <summary>
        /// Loads an <see cref="T:Newtonsoft.Json.Linq.JArray" /> from a <see cref="T:Newtonsoft.Json.JsonReader" />.
        /// </summary>
        /// <param name="reader">A <see cref="T:Newtonsoft.Json.JsonReader" /> that will be read for the content of the <see cref="T:Newtonsoft.Json.Linq.JArray" />.</param>
        /// <param name="settings">The <see cref="T:Newtonsoft.Json.Linq.JsonLoadSettings" /> used to load the JSON.
        /// If this is null, default load settings will be used.</param>
        /// <returns>A <see cref="T:Newtonsoft.Json.Linq.JArray" /> that contains the JSON that was read from the specified <see cref="T:Newtonsoft.Json.JsonReader" />.</returns>
        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);
        }
示例#17
0
        internal async Task ReadTokenFromAsync(JsonReader reader, JsonLoadSettings options, CancellationToken cancellationToken = default)
        {
            ValidationUtils.ArgumentNotNull(reader, nameof(reader));
            int startDepth = reader.Depth;

            if (!await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
            {
                throw JsonReaderException.Create(reader, "Error reading {0} from JsonReader.".FormatWith(CultureInfo.InvariantCulture, GetType().Name));
            }

            await ReadContentFromAsync(reader, options, cancellationToken).ConfigureAwait(false);

            if (reader.Depth > startDepth)
            {
                throw JsonReaderException.Create(reader, "Unexpected end of content while loading {0}.".FormatWith(CultureInfo.InvariantCulture, GetType().Name));
            }
        }
示例#18
0
    public void test()
    {
        MonsterTemplate m = new MonsterTemplate();

        m.baseHP = 5;
        m.name   = "Jane";
        m.maxAtk = 9;
        m.minAtk = 2;
        m.id     = 33;
        m.level  = 2;

        Newtonsoft.Json.Linq.JsonLoadSettings setting = new Newtonsoft.Json.Linq.JsonLoadSettings();

        Debug.Log("---");
        string jsonStr = JsonConvert.SerializeObject(m);

        Debug.Log(jsonStr);

        //Newtonsoft.Json.Linq.JObject obj = JsonConvert.
    }
        internal void ReadContentFrom(JsonReader r, JsonLoadSettings settings)
        {
            JValue jValue;
            bool   value;

            ValidationUtils.ArgumentNotNull(r, "r");
            IJsonLineInfo jsonLineInfo = r as IJsonLineInfo;
            JContainer    parent       = this;

            do
            {
                JProperty jProperty = parent as JProperty;
                if (jProperty != null)
                {
                    value = jProperty.Value;
                }
                else
                {
                    value = false;
                }
                if (value)
                {
                    if (parent == this)
                    {
                        return;
                    }
                    parent = parent.Parent;
                }
                switch (r.TokenType)
                {
                case JsonToken.None:
                {
                    continue;
                }

                case JsonToken.StartObject:
                {
                    JObject jObjects = new JObject();
                    jObjects.SetLineInfo(jsonLineInfo, settings);
                    parent.Add(jObjects);
                    parent = jObjects;
                    continue;
                }

                case JsonToken.StartArray:
                {
                    JArray jArrays = new JArray();
                    jArrays.SetLineInfo(jsonLineInfo, settings);
                    parent.Add(jArrays);
                    parent = jArrays;
                    continue;
                }

                case JsonToken.StartConstructor:
                {
                    JConstructor jConstructor = new JConstructor(r.Value.ToString());
                    jConstructor.SetLineInfo(jsonLineInfo, settings);
                    parent.Add(jConstructor);
                    parent = jConstructor;
                    continue;
                }

                case JsonToken.PropertyName:
                {
                    string    str        = r.Value.ToString();
                    JProperty jProperty1 = new JProperty(str);
                    jProperty1.SetLineInfo(jsonLineInfo, settings);
                    JProperty jProperty2 = ((JObject)parent).Property(str);
                    if (jProperty2 != null)
                    {
                        jProperty2.Replace(jProperty1);
                    }
                    else
                    {
                        parent.Add(jProperty1);
                    }
                    parent = jProperty1;
                    continue;
                }

                case JsonToken.Comment:
                {
                    if (settings == null || settings.CommentHandling != CommentHandling.Load)
                    {
                        continue;
                    }
                    jValue = JValue.CreateComment(r.Value.ToString());
                    jValue.SetLineInfo(jsonLineInfo, settings);
                    parent.Add(jValue);
                    continue;
                }

                case JsonToken.Raw:
                {
                    throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".FormatWith(CultureInfo.InvariantCulture, r.TokenType));
                }

                case JsonToken.Integer:
                case JsonToken.Float:
                case JsonToken.String:
                case JsonToken.Boolean:
                case JsonToken.Date:
                case JsonToken.Bytes:
                {
                    jValue = new JValue(r.Value);
                    jValue.SetLineInfo(jsonLineInfo, settings);
                    parent.Add(jValue);
                    continue;
                }

                case JsonToken.Null:
                {
                    jValue = JValue.CreateNull();
                    jValue.SetLineInfo(jsonLineInfo, settings);
                    parent.Add(jValue);
                    continue;
                }

                case JsonToken.Undefined:
                {
                    jValue = JValue.CreateUndefined();
                    jValue.SetLineInfo(jsonLineInfo, settings);
                    parent.Add(jValue);
                    continue;
                }

                case JsonToken.EndObject:
                {
                    if (parent == this)
                    {
                        return;
                    }
                    parent = parent.Parent;
                    continue;
                }

                case JsonToken.EndArray:
                {
                    if (parent == this)
                    {
                        return;
                    }
                    parent = parent.Parent;
                    continue;
                }

                case JsonToken.EndConstructor:
                {
                    if (parent == this)
                    {
                        return;
                    }
                    parent = parent.Parent;
                    continue;
                }

                default:
                {
                    throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".FormatWith(CultureInfo.InvariantCulture, r.TokenType));
                }
                }
            }while (r.Read());
        }
示例#20
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 is JProperty jproperty ? jproperty.Value : (JToken)null) != 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());
        }
示例#21
0
 /// <summary>
 /// Asynchronously creates a <see cref="JToken"/> from a <see cref="JsonReader"/>.
 /// </summary>
 /// <param name="reader">A <see cref="JsonReader"/> positioned at the token to read into this <see cref="JToken"/>.</param>
 /// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
 /// If this is <c>null</c>, default load settings will be used.</param>
 /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
 /// <returns>
 /// A <see cref="Task{TResult}"/> that represents the asynchronous creation. The <see cref="Task{TResult}.Result"/>
 /// property returns a <see cref="JToken"/> that contains the token and its descendant tokens
 /// that were read from the reader. The runtime type of the token is determined
 /// by the token type of the first token encountered in the reader.
 /// </returns>
 public static Task <JToken> LoadAsync(JsonReader reader, JsonLoadSettings settings, CancellationToken cancellationToken = default(CancellationToken))
 {
     return(ReadFromAsync(reader, settings, cancellationToken));
 }
示例#22
0
        /// <summary>
        /// Loads an <see cref="JConstructor"/> from a <see cref="JsonReader"/>. 
        /// </summary>
        /// <param name="reader">A <see cref="JsonReader"/> that will be read for the content of the <see cref="JConstructor"/>.</param>
        /// <param name="settings">The <see cref="JsonLoadSettings"/> used to load the JSON.
        /// If this is null, default load settings will be used.</param>
        /// <returns>A <see cref="JConstructor"/> that contains the JSON that was read from the specified <see cref="JsonReader"/>.</returns>
        public new static JConstructor Load(JsonReader reader, JsonLoadSettings settings)
        {
            if (reader.TokenType == JsonToken.None)
            {
                if (!reader.Read())
                    throw JsonReaderException.Create(reader, "Error reading JConstructor from JsonReader.");
            }

            while (reader.TokenType == JsonToken.Comment)
            {
                reader.Read();
            }

            if (reader.TokenType != JsonToken.StartConstructor)
                throw JsonReaderException.Create(reader, "Error reading JConstructor from JsonReader. Current JsonReader item is not a constructor: {0}".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));

            JConstructor c = new JConstructor((string)reader.Value);
            c.SetLineInfo(reader as IJsonLineInfo);

            c.ReadTokenFrom(reader, settings);

            return c;
        }
示例#23
0
        private async Task ReadContentFromAsync(JsonReader reader, JsonLoadSettings settings, CancellationToken cancellationToken = default)
        {
            IJsonLineInfo lineInfo = reader as IJsonLineInfo;

            JContainer parent = this;

            do
            {
                if (parent is JProperty p && p.Value != null)
                {
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                }

                switch (reader.TokenType)
                {
                case JsonToken.None:
                    // new reader. move to actual content
                    break;

                case JsonToken.StartArray:
                    JArray a = new JArray();
                    a.SetLineInfo(lineInfo, settings);
                    parent.Add(a);
                    parent = a;
                    break;

                case JsonToken.EndArray:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.StartObject:
                    JObject o = new JObject();
                    o.SetLineInfo(lineInfo, settings);
                    parent.Add(o);
                    parent = o;
                    break;

                case JsonToken.EndObject:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.StartConstructor:
                    JConstructor constructor = new JConstructor(reader.Value.ToString());
                    constructor.SetLineInfo(lineInfo, settings);
                    parent.Add(constructor);
                    parent = constructor;
                    break;

                case JsonToken.EndConstructor:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.String:
                case JsonToken.Integer:
                case JsonToken.Float:
                case JsonToken.Date:
                case JsonToken.Boolean:
                case JsonToken.Bytes:
                    JValue v = new JValue(reader.Value);
                    v.SetLineInfo(lineInfo, settings);
                    parent.Add(v);
                    break;

                case JsonToken.Comment:
                    if (settings != null && settings.CommentHandling == CommentHandling.Load)
                    {
                        v = JValue.CreateComment(reader.Value.ToString());
                        v.SetLineInfo(lineInfo, settings);
                        parent.Add(v);
                    }
                    break;

                case JsonToken.Null:
                    v = JValue.CreateNull();
                    v.SetLineInfo(lineInfo, settings);
                    parent.Add(v);
                    break;

                case JsonToken.Undefined:
                    v = JValue.CreateUndefined();
                    v.SetLineInfo(lineInfo, settings);
                    parent.Add(v);
                    break;

                case JsonToken.PropertyName:
                    JProperty property = ReadProperty(reader, settings, lineInfo, parent);
                    if (property != null)
                    {
                        parent = property;
                    }
                    else
                    {
                        await reader.SkipAsync();
                    }
                    break;

                default:
                    throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
                }
            } while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false));
        }
示例#24
0
        internal void ReadContentFrom(JsonReader r, JsonLoadSettings settings)
        {
            JProperty property1;
            JValue    value2;
            JProperty property;

            ValidationUtils.ArgumentNotNull(r, "r");
            IJsonLineInfo lineInfo = r as IJsonLineInfo;
            JContainer    parent   = this;

Label_0014:
            property1 = parent as JProperty;
            if (property1 == null)
            {
            }
            if (null.Value != null)
            {
                if (parent == this)
                {
                    return;
                }
                parent = parent.Parent;
            }
            switch (r.TokenType)
            {
            case JsonToken.None:
                goto Label_0247;

            case JsonToken.StartObject:
            {
                JObject content = new JObject();
                content.SetLineInfo(lineInfo, settings);
                parent.Add(content);
                parent = content;
                goto Label_0247;
            }

            case JsonToken.StartArray:
            {
                JArray content = new JArray();
                content.SetLineInfo(lineInfo, settings);
                parent.Add(content);
                parent = content;
                goto Label_0247;
            }

            case JsonToken.StartConstructor:
            {
                JConstructor content = new JConstructor(r.Value.ToString());
                content.SetLineInfo(lineInfo, settings);
                parent.Add(content);
                parent = content;
                goto Label_0247;
            }

            case JsonToken.PropertyName:
            {
                string name = r.Value.ToString();
                property = new JProperty(name);
                property.SetLineInfo(lineInfo, settings);
                JProperty property2 = ((JObject)parent).Property(name);
                if (property2 != null)
                {
                    property2.Replace(property);
                    break;
                }
                parent.Add(property);
                break;
            }

            case JsonToken.Comment:
                if ((settings != null) && (settings.CommentHandling == CommentHandling.Load))
                {
                    value2 = JValue.CreateComment(r.Value.ToString());
                    value2.SetLineInfo(lineInfo, settings);
                    parent.Add(value2);
                }
                goto Label_0247;

            case JsonToken.Integer:
            case JsonToken.Float:
            case JsonToken.String:
            case JsonToken.Boolean:
            case JsonToken.Date:
            case JsonToken.Bytes:
                value2 = new JValue(r.Value);
                value2.SetLineInfo(lineInfo, settings);
                parent.Add(value2);
                goto Label_0247;

            case JsonToken.Null:
                value2 = JValue.CreateNull();
                value2.SetLineInfo(lineInfo, settings);
                parent.Add(value2);
                goto Label_0247;

            case JsonToken.Undefined:
                value2 = JValue.CreateUndefined();
                value2.SetLineInfo(lineInfo, settings);
                parent.Add(value2);
                goto Label_0247;

            case JsonToken.EndObject:
                if (parent != this)
                {
                    parent = parent.Parent;
                    goto Label_0247;
                }
                return;

            case JsonToken.EndArray:
                if (parent != this)
                {
                    parent = parent.Parent;
                    goto Label_0247;
                }
                return;

            case JsonToken.EndConstructor:
                if (parent != this)
                {
                    parent = parent.Parent;
                    goto Label_0247;
                }
                return;

            default:
                throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".FormatWith(CultureInfo.InvariantCulture, r.TokenType));
            }
            parent = property;
Label_0247:
            if (r.Read())
            {
                goto Label_0014;
            }
        }
示例#25
0
        // Token: 0x06001269 RID: 4713 RVA: 0x000648AC File Offset: 0x00062AAC
        internal void ReadContentFrom(JsonReader r, [Nullable(2)] JsonLoadSettings settings)
        {
            ValidationUtils.ArgumentNotNull(r, "r");
            IJsonLineInfo lineInfo   = r as IJsonLineInfo;
            JContainer    jcontainer = this;

            for (;;)
            {
                JProperty jproperty = jcontainer as JProperty;
                if (jproperty != null && jproperty.Value != null)
                {
                    if (jcontainer == this)
                    {
                        break;
                    }
                    jcontainer = jcontainer.Parent;
                }
                switch (r.TokenType)
                {
                case JsonToken.None:
                    goto IL_218;

                case JsonToken.StartObject:
                {
                    JObject jobject = new JObject();
                    jobject.SetLineInfo(lineInfo, settings);
                    jcontainer.Add(jobject);
                    jcontainer = jobject;
                    goto IL_218;
                }

                case JsonToken.StartArray:
                {
                    JArray jarray = new JArray();
                    jarray.SetLineInfo(lineInfo, settings);
                    jcontainer.Add(jarray);
                    jcontainer = jarray;
                    goto IL_218;
                }

                case JsonToken.StartConstructor:
                {
                    JConstructor jconstructor = new JConstructor(r.Value.ToString());
                    jconstructor.SetLineInfo(lineInfo, settings);
                    jcontainer.Add(jconstructor);
                    jcontainer = jconstructor;
                    goto IL_218;
                }

                case JsonToken.PropertyName:
                {
                    JProperty jproperty2 = JContainer.ReadProperty(r, settings, lineInfo, jcontainer);
                    if (jproperty2 != null)
                    {
                        jcontainer = jproperty2;
                        goto IL_218;
                    }
                    r.Skip();
                    goto IL_218;
                }

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

                case JsonToken.Integer:
                case JsonToken.Float:
                case JsonToken.String:
                case JsonToken.Boolean:
                case JsonToken.Date:
                case JsonToken.Bytes:
                {
                    JValue jvalue = new JValue(r.Value);
                    jvalue.SetLineInfo(lineInfo, settings);
                    jcontainer.Add(jvalue);
                    goto IL_218;
                }

                case JsonToken.Null:
                {
                    JValue jvalue = JValue.CreateNull();
                    jvalue.SetLineInfo(lineInfo, settings);
                    jcontainer.Add(jvalue);
                    goto IL_218;
                }

                case JsonToken.Undefined:
                {
                    JValue jvalue = JValue.CreateUndefined();
                    jvalue.SetLineInfo(lineInfo, settings);
                    jcontainer.Add(jvalue);
                    goto IL_218;
                }

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

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

                case JsonToken.EndConstructor:
                    if (jcontainer == this)
                    {
                        return;
                    }
                    jcontainer = jcontainer.Parent;
                    goto IL_218;
                }
                goto Block_4;
IL_218:
                if (!r.Read())
                {
                    return;
                }
            }
            return;

Block_4:
            throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".FormatWith(CultureInfo.InvariantCulture, r.TokenType));
        }
示例#26
0
        internal void InitializeDataSourceStream(Stream data)
        {
            //try to deserialize to QueryComposer response, on fail use generic grid view
            try
            {
                var serializationSettings = new Newtonsoft.Json.JsonSerializerSettings();
                serializationSettings.Converters.Add(new DTO.QueryComposer.QueryComposerResponsePropertyDefinitionConverter());
                var deserializer = Newtonsoft.Json.JsonSerializer.Create(serializationSettings);
                var loadSettings = new Newtonsoft.Json.Linq.JsonLoadSettings {
                    CommentHandling = Newtonsoft.Json.Linq.CommentHandling.Ignore, LineInfoHandling = Newtonsoft.Json.Linq.LineInfoHandling.Ignore
                };

                DTO.QueryComposer.QueryComposerResponseDTO responseDTO = null;

                var jObj = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.Linq.JObject.ReadFrom(new Newtonsoft.Json.JsonTextReader(new StreamReader(data)), loadSettings);

                if (jObj.ContainsKey("SchemaVersion"))
                {
                    responseDTO = jObj.ToObject <DTO.QueryComposer.QueryComposerResponseDTO>(deserializer);
                }
                else
                {
                    //assume it is pre multi-query response
                    var queryDTO = jObj.ToObject <DTO.QueryComposer.QueryComposerResponseQueryResultDTO>(deserializer);

                    responseDTO = new DTO.QueryComposer.QueryComposerResponseDTO
                    {
                        Queries = new[] { queryDTO }
                    };
                }

                HasResults = responseDTO.Queries.SelectMany(q => { return(q.Results == null ? new int[] { 0 } : q.Results.Select(r => r.Count())); }).Sum() > 0;

                this.SuspendLayout();
                this.Controls.Clear();

                int yPosition = 0;
                int index     = 0;
                foreach (var queryResult in responseDTO.Queries)
                {
                    var titleLabel = new Label();
                    titleLabel.AutoSize  = true;
                    titleLabel.Anchor    = AnchorStyles.Left | AnchorStyles.Top;
                    titleLabel.TextAlign = ContentAlignment.MiddleLeft;
                    titleLabel.AutoSize  = false;
                    titleLabel.Size      = new Size(this.Width, 23);
                    titleLabel.Location  = new Point(0, 0);
                    titleLabel.Padding   = new Padding(3);
                    titleLabel.Name      = $"lblTitle_{index}";
                    titleLabel.Text      = $"Cohort {index + 1}: {queryResult.Name}";
                    titleLabel.ForeColor = SystemColors.InactiveCaptionText;

                    var resultGrid = CreateDataGridView("grid" + index, FromQueryComposerQueryResult(queryResult));
                    resultGrid.Location = new Point(0, titleLabel.Height);
                    resultGrid.Dock     = DockStyle.None;
                    resultGrid.Anchor   = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;

                    var totalNumberOfResultRows = (from rs in queryResult.Results
                                                   select rs.Count()).Sum();

                    var gridHeight = resultGrid.ColumnHeadersHeight + (totalNumberOfResultRows * resultGrid.RowTemplate.Height);

                    if (gridHeight < 150)
                    {
                        gridHeight = 150;
                    }
                    resultGrid.Size = new Size(this.Width, gridHeight);

                    var layoutPanel = new Panel();
                    layoutPanel.Anchor    = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
                    layoutPanel.Name      = "layoutPanel" + index;
                    layoutPanel.Size      = new Size(this.Width, resultGrid.Bottom);
                    layoutPanel.TabIndex  = index;
                    layoutPanel.BackColor = SystemColors.GradientInactiveCaption;
                    layoutPanel.Location  = new Point(0, yPosition);
                    layoutPanel.Controls.AddRange(new Control[] { resultGrid, titleLabel });
                    yPosition += layoutPanel.ClientRectangle.Height;

                    this.Controls.Add(layoutPanel);

                    index++;
                }


                if (this.Controls.Count == 1)
                {
                    this.Controls[0].Size   = new Size(this.Controls[0].Size.Width, this.Size.Height);
                    this.Controls[0].Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;

                    ((Panel)this.Controls[0]).PerformLayout();
                }
                else
                {
                    this.Size = new Size(this.Width, yPosition);
                }

                this.ResumeLayout();
            }
            catch (Exception ex)
            {
                using (var reader = new StreamReader(data))
                {
                    var grid = CreateDataGridView("response1", TransformJSONToDataTable(reader.ReadToEnd()));

                    this.Controls.Clear();
                    this.Controls.Add(grid);
                }
            }
        }
示例#27
0
        internal void ReadContentFrom(JsonReader r, JsonLoadSettings settings)
        {
            ValidationUtils.ArgumentNotNull(r, nameof(r));
            IJsonLineInfo lineInfo = r as IJsonLineInfo;

            JContainer parent = this;

            do
            {
                if (parent is JProperty && ((JProperty)parent).Value != null)
                {
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                }

                switch (r.TokenType)
                {
                case JsonToken.None:
                    // new reader. move to actual content
                    break;

                case JsonToken.StartArray:
                    JArray a = new JArray();
                    a.SetLineInfo(lineInfo, settings);
                    parent.Add(a);
                    parent = a;
                    break;

                case JsonToken.EndArray:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.StartObject:
                    JObject o = new JObject();
                    o.SetLineInfo(lineInfo, settings);
                    parent.Add(o);
                    parent = o;
                    break;

                case JsonToken.EndObject:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.StartConstructor:
                    JConstructor constructor = new JConstructor(r.Value.ToString());
                    constructor.SetLineInfo(lineInfo, settings);
                    parent.Add(constructor);
                    parent = constructor;
                    break;

                case JsonToken.EndConstructor:
                    if (parent == this)
                    {
                        return;
                    }

                    parent = parent.Parent;
                    break;

                case JsonToken.String:
                case JsonToken.Integer:
                case JsonToken.Float:
                case JsonToken.Date:
                case JsonToken.Boolean:
                case JsonToken.Bytes:
                    JValue v = new JValue(r.Value);
                    v.SetLineInfo(lineInfo, settings);
                    parent.Add(v);
                    break;

                case JsonToken.Comment:
                    if (settings != null && settings.CommentHandling == CommentHandling.Load)
                    {
                        v = JValue.CreateComment(r.Value.ToString());
                        v.SetLineInfo(lineInfo, settings);
                        parent.Add(v);
                    }
                    break;

                case JsonToken.Null:
                    v = JValue.CreateNull();
                    v.SetLineInfo(lineInfo, settings);
                    parent.Add(v);
                    break;

                case JsonToken.Undefined:
                    v = JValue.CreateUndefined();
                    v.SetLineInfo(lineInfo, settings);
                    parent.Add(v);
                    break;

                case JsonToken.PropertyName:
                    string    propertyName = r.Value.ToString();
                    JProperty property     = new JProperty(propertyName);
                    property.SetLineInfo(lineInfo, settings);
                    JObject parentObject = (JObject)parent;
                    // handle multiple properties with the same name in JSON
                    JProperty existingPropertyWithName = parentObject.Property(propertyName);
                    if (existingPropertyWithName == null)
                    {
                        parent.Add(property);
                    }
                    else
                    {
                        existingPropertyWithName.Replace(property);
                    }
                    parent = property;
                    break;

                default:
                    throw new InvalidOperationException("The JsonReader should not be on a token of type {0}.".FormatWith(CultureInfo.InvariantCulture, r.TokenType));
                }
            } while (r.Read());
        }
示例#28
0
文件: JsonNET.cs 项目: safl/ql
 public static JObject Parse(string str, JsonLoadSettings settings) => null;