Пример #1
0
 public static UnhandledJsonTokenException GetUnknown(NJL.JToken jToken)
 => throw new UnhandledJsonTokenException(jToken, $"Factory for {jToken.GetType()} not implemented.");
Пример #2
0
        internal JToken Evaluate(JToken root, bool errorWhenNoMatch)
        {
            JToken current = root;

            foreach (object part in Parts)
            {
                string propertyName = part as string;
                if (propertyName != null)
                {
                    JObject o = current as JObject;
                    if (o != null)
                    {
                        current = o[propertyName];

                        if (current == null && errorWhenNoMatch)
                        {
                            throw new JsonException("Property '{0}' does not exist on JObject.".FormatWith(CultureInfo.InvariantCulture, propertyName));
                        }
                    }
                    else
                    {
                        if (errorWhenNoMatch)
                        {
                            throw new JsonException("Property '{0}' not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, propertyName, current.GetType().Name));
                        }

                        return(null);
                    }
                }
                else
                {
                    int index = (int)part;

                    JArray       a = current as JArray;
                    JConstructor c = current as JConstructor;

                    if (a != null)
                    {
                        if (a.Count <= index)
                        {
                            if (errorWhenNoMatch)
                            {
                                throw new JsonException("Index {0} outside the bounds of JArray.".FormatWith(CultureInfo.InvariantCulture, index));
                            }

                            return(null);
                        }

                        current = a[index];
                    }
                    else if (c != null)
                    {
                        if (c.Count <= index)
                        {
                            if (errorWhenNoMatch)
                            {
                                throw new JsonException("Index {0} outside the bounds of JConstructor.".FormatWith(CultureInfo.InvariantCulture, index));
                            }

                            return(null);
                        }

                        current = c[index];
                    }
                    else
                    {
                        if (errorWhenNoMatch)
                        {
                            throw new JsonException("Index {0} not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, index, current.GetType().Name));
                        }

                        return(null);
                    }
                }
            }

            return(current);
        }
Пример #3
0
        internal JToken Evaluate(JToken root, bool errorWhenNoMatch)
        {
            JToken jtoken = root;

            foreach (object obj in this.Parts)
            {
                string text = obj as string;
                if (text != null)
                {
                    JObject jobject = jtoken as JObject;
                    if (jobject != null)
                    {
                        jtoken = jobject[text];
                        if (jtoken == null && errorWhenNoMatch)
                        {
                            throw new Exception("Property '{0}' does not exist on JObject.".FormatWith(CultureInfo.InvariantCulture, new object[]
                            {
                                text
                            }));
                        }
                    }
                    else
                    {
                        if (errorWhenNoMatch)
                        {
                            throw new Exception("Property '{0}' not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, new object[]
                            {
                                text,
                                jtoken.GetType().Name
                            }));
                        }
                        return(null);
                    }
                }
                else
                {
                    int    num    = (int)obj;
                    JArray jarray = jtoken as JArray;
                    if (jarray != null)
                    {
                        if (jarray.Count <= num)
                        {
                            if (errorWhenNoMatch)
                            {
                                throw new IndexOutOfRangeException("Index {0} outside the bounds of JArray.".FormatWith(CultureInfo.InvariantCulture, new object[]
                                {
                                    num
                                }));
                            }
                            return(null);
                        }
                        else
                        {
                            jtoken = jarray[num];
                        }
                    }
                    else
                    {
                        if (errorWhenNoMatch)
                        {
                            throw new Exception("Index {0} not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, new object[]
                            {
                                num,
                                jtoken.GetType().Name
                            }));
                        }
                        return(null);
                    }
                }
            }
            return(jtoken);
        }
Пример #4
0
        internal JToken Evaluate(JToken root, bool errorWhenNoMatch)
        {
            JToken jTokens;
            JToken item = root;

            List <object> .Enumerator enumerator = this.Parts.GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    object current = enumerator.Current;
                    string str     = current as string;
                    if (str == null)
                    {
                        int    num     = (int)current;
                        JArray jArrays = item as JArray;
                        if (jArrays == null)
                        {
                            if (errorWhenNoMatch)
                            {
                                throw new Exception("Index {0} not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, new object[] { num, item.GetType().Name }));
                            }
                            jTokens = null;
                            return(jTokens);
                        }
                        else if (jArrays.Count > num)
                        {
                            item = jArrays[num];
                        }
                        else
                        {
                            if (errorWhenNoMatch)
                            {
                                throw new IndexOutOfRangeException("Index {0} outside the bounds of JArray.".FormatWith(CultureInfo.InvariantCulture, new object[] { num }));
                            }
                            jTokens = null;
                            return(jTokens);
                        }
                    }
                    else
                    {
                        JObject jObjects = item as JObject;
                        if (jObjects == null)
                        {
                            if (errorWhenNoMatch)
                            {
                                throw new Exception("Property '{0}' not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, new object[] { str, item.GetType().Name }));
                            }
                            jTokens = null;
                            return(jTokens);
                        }
                        else
                        {
                            item = jObjects[str];
                            if (item == null && errorWhenNoMatch)
                            {
                                throw new Exception("Property '{0}' does not exist on JObject.".FormatWith(CultureInfo.InvariantCulture, new object[] { str }));
                            }
                        }
                    }
                }
                return(item);
            }
            finally
            {
                ((IDisposable)enumerator).Dispose();
            }
            return(jTokens);
        }
Пример #5
0
        internal override void ValidateToken(JToken o, JToken?existing)
        {
            ValidationUtils.ArgumentNotNull(o, nameof(o));

            if (o.Type != JTokenType.Property)
            {
                throw new ArgumentException("Can not add {0} to {1}.".FormatWith(CultureInfo.InvariantCulture, o.GetType(), GetType()));
            }

            JProperty newProperty = (JProperty)o;

            if (existing != null)
            {
                JProperty existingProperty = (JProperty)existing;

                if (newProperty.Name == existingProperty.Name)
                {
                    return;
                }
            }

            if (_properties.TryGetValue(newProperty.Name, out existing))
            {
                throw new ArgumentException("Can not add property {0} to {1}. Property with the same name already exists on object.".FormatWith(CultureInfo.InvariantCulture, newProperty.Name, GetType()));
            }
        }
Пример #6
0
        internal JToken Evaluate(JToken root, bool errorWhenNoMatch)
        {
            JToken jToken = root;

            foreach (object part in Parts)
            {
                string text = part as string;
                if (text != null)
                {
                    JObject jObject = jToken as JObject;
                    if (jObject != null)
                    {
                        jToken = jObject[text];
                        if (jToken == null && errorWhenNoMatch)
                        {
                            throw new Exception("Property '{0}' does not exist on JObject.".FormatWith(CultureInfo.InvariantCulture, text));
                        }
                        continue;
                    }
                    if (errorWhenNoMatch)
                    {
                        throw new Exception("Property '{0}' not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, text, jToken.GetType().Name));
                    }
                    return(null);
                }
                int    num    = (int)part;
                JArray jArray = jToken as JArray;
                if (jArray != null)
                {
                    if (jArray.Count <= num)
                    {
                        if (errorWhenNoMatch)
                        {
                            throw new IndexOutOfRangeException("Index {0} outside the bounds of JArray.".FormatWith(CultureInfo.InvariantCulture, num));
                        }
                        return(null);
                    }
                    jToken = jArray[num];
                    continue;
                }
                if (errorWhenNoMatch)
                {
                    throw new Exception("Index {0} not valid on {1}.".FormatWith(CultureInfo.InvariantCulture, num, jToken.GetType().Name));
                }
                return(null);
            }
            return(jToken);
        }
Пример #7
0
        internal virtual void ValidateToken(JToken o)
        {
            ValidationUtils.ArgumentNotNull(o, "o");

            if (o.Type == JsonTokenType.Property)
            {
                throw new Exception("Can not add {0} to {1}".FormatWith(CultureInfo.InvariantCulture, o.GetType(), GetType()));
            }
        }