コード例 #1
0
        private void WriteToken(IList <JsonSchemaModel> schemas)
        {
            foreach (SchemaScope schemaScope in _stack)
            {
                bool isInUniqueArray = (schemaScope.TokenType == JTokenType.Array && schemaScope.IsUniqueArray && schemaScope.ArrayItemCount > 0);

                if (isInUniqueArray || schemaScope.IsEnum || schemas.Any(s => s.Enum != null))
                {
                    if (schemaScope.CurrentItemWriter == null)
                    {
                        if (JsonWriter.IsEndToken(_reader.TokenType))
                        {
                            continue;
                        }

                        schemaScope.CurrentItemWriter = new JTokenWriter();
                    }

                    schemaScope.CurrentItemWriter.WriteToken(_reader, false);

                    // finished writing current item
                    if (schemaScope.CurrentItemWriter.Top == 0 && _reader.TokenType != JsonToken.PropertyName)
                    {
                        JToken finishedItem = schemaScope.CurrentItemWriter.Token;

                        // start next item with new writer
                        schemaScope.CurrentItemWriter = null;

                        if (isInUniqueArray)
                        {
                            if (schemaScope.UniqueArrayItems.Contains(finishedItem, JToken.EqualityComparer))
                            {
                                RaiseError("Non-unique array item at index {0}.".FormatWith(CultureInfo.InvariantCulture, schemaScope.ArrayItemCount - 1), schemaScope.Schemas.First(s => s.UniqueItems));
                            }

                            schemaScope.UniqueArrayItems.Add(finishedItem);
                        }
                        else if (schemaScope.IsEnum || schemas.Any(s => s.Enum != null))
                        {
                            foreach (JsonSchemaModel schema in schemas)
                            {
                                if (schema.Enum != null)
                                {
                                    if (!schema.Enum.ContainsValue(finishedItem, JToken.EqualityComparer))
                                    {
                                        StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
                                        finishedItem.WriteTo(new JsonTextWriter(sw));

                                        RaiseError("Value {0} is not defined in enum.".FormatWith(CultureInfo.InvariantCulture, sw.ToString()), schema);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
コード例 #2
0
 private void WriteToken(IList <JsonSchemaModel> schemas)
 {
     foreach (JsonValidatingReader.SchemaScope current in this._stack)
     {
         bool flag = current.TokenType == JTokenType.Array && current.IsUniqueArray && current.ArrayItemCount > 0;
         if (!flag && !current.IsEnum)
         {
             if (!schemas.Any((JsonSchemaModel s) => s.Enum != null))
             {
                 continue;
             }
         }
         if (current.CurrentItemWriter == null)
         {
             if (JsonWriter.IsEndToken(this._reader.TokenType))
             {
                 continue;
             }
             current.CurrentItemWriter = new JTokenWriter();
         }
         current.CurrentItemWriter.WriteToken(this._reader, false);
         if (current.CurrentItemWriter.Top == 0 && this._reader.TokenType != JsonToken.PropertyName)
         {
             JToken token = current.CurrentItemWriter.Token;
             current.CurrentItemWriter = null;
             if (flag)
             {
                 if (current.UniqueArrayItems.Contains(token, JToken.EqualityComparer))
                 {
                     this.RaiseError("Non-unique array item at index {0}.".FormatWith(CultureInfo.InvariantCulture, current.ArrayItemCount - 1), current.Schemas.First((JsonSchemaModel s) => s.UniqueItems));
                 }
                 current.UniqueArrayItems.Add(token);
             }
             else
             {
                 if (!current.IsEnum)
                 {
                     if (!schemas.Any((JsonSchemaModel s) => s.Enum != null))
                     {
                         continue;
                     }
                 }
                 foreach (JsonSchemaModel current2 in schemas)
                 {
                     if (current2.Enum != null && !current2.Enum.ContainsValue(token, JToken.EqualityComparer))
                     {
                         StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture);
                         token.WriteTo(new JsonTextWriter(stringWriter), new JsonConverter[0]);
                         this.RaiseError("Value {0} is not defined in enum.".FormatWith(CultureInfo.InvariantCulture, stringWriter.ToString()), current2);
                     }
                 }
             }
         }
     }
 }
コード例 #3
0
        internal void WriteToken(JsonReader reader, int initialDepth, bool writeChildren, bool writeDateConstructorAsDate)
        {
            while (true)
            {
                switch (reader.TokenType)
                {
                case JsonToken.None:
                    goto IL_2AB;

                case JsonToken.StartObject:
                    this.WriteStartObject();
                    goto IL_2AB;

                case JsonToken.StartArray:
                    this.WriteStartArray();
                    goto IL_2AB;

                case JsonToken.StartConstructor:
                {
                    string a = reader.Value.ToString();
                    if (writeDateConstructorAsDate && string.Equals(a, "Date", StringComparison.Ordinal))
                    {
                        this.WriteConstructorDate(reader);
                        goto IL_2AB;
                    }
                    this.WriteStartConstructor(reader.Value.ToString());
                    goto IL_2AB;
                }

                case JsonToken.PropertyName:
                    this.WritePropertyName(reader.Value.ToString());
                    goto IL_2AB;

                case JsonToken.Comment:
                    this.WriteComment((reader.Value != null) ? reader.Value.ToString() : null);
                    goto IL_2AB;

                case JsonToken.Raw:
                    this.WriteRawValue((reader.Value != null) ? reader.Value.ToString() : null);
                    goto IL_2AB;

                case JsonToken.Integer:
                    if (reader.Value is BigInteger)
                    {
                        this.WriteValue((BigInteger)reader.Value);
                        goto IL_2AB;
                    }
                    this.WriteValue(Convert.ToInt64(reader.Value, CultureInfo.InvariantCulture));
                    goto IL_2AB;

                case JsonToken.Float:
                {
                    object value = reader.Value;
                    if (value is decimal)
                    {
                        this.WriteValue((decimal)value);
                        goto IL_2AB;
                    }
                    if (value is double)
                    {
                        this.WriteValue((double)value);
                        goto IL_2AB;
                    }
                    if (value is float)
                    {
                        this.WriteValue((float)value);
                        goto IL_2AB;
                    }
                    this.WriteValue(Convert.ToDouble(value, CultureInfo.InvariantCulture));
                    goto IL_2AB;
                }

                case JsonToken.String:
                    this.WriteValue(reader.Value.ToString());
                    goto IL_2AB;

                case JsonToken.Boolean:
                    this.WriteValue(Convert.ToBoolean(reader.Value, CultureInfo.InvariantCulture));
                    goto IL_2AB;

                case JsonToken.Null:
                    this.WriteNull();
                    goto IL_2AB;

                case JsonToken.Undefined:
                    this.WriteUndefined();
                    goto IL_2AB;

                case JsonToken.EndObject:
                    this.WriteEndObject();
                    goto IL_2AB;

                case JsonToken.EndArray:
                    this.WriteEndArray();
                    goto IL_2AB;

                case JsonToken.EndConstructor:
                    this.WriteEndConstructor();
                    goto IL_2AB;

                case JsonToken.Date:
                    if (reader.Value is DateTimeOffset)
                    {
                        this.WriteValue((DateTimeOffset)reader.Value);
                        goto IL_2AB;
                    }
                    this.WriteValue(Convert.ToDateTime(reader.Value, CultureInfo.InvariantCulture));
                    goto IL_2AB;

                case JsonToken.Bytes:
                    this.WriteValue((byte[])reader.Value);
                    goto IL_2AB;
                }
                break;
IL_2AB:
                if (initialDepth - 1 >= reader.Depth - (JsonWriter.IsEndToken(reader.TokenType) ? 1 : 0) || !writeChildren || !reader.Read())
                {
                    return;
                }
            }
            throw MiscellaneousUtils.CreateArgumentOutOfRangeException("TokenType", reader.TokenType, "Unexpected token type.");
        }