Пример #1
0
        private void WriteType(string propertyName, JsonWriter writer, JSchemaType type)
        {
            IList <JSchemaType> types;

            if (Enum.IsDefined(typeof(JSchemaType), type))
            {
                types = new List <JSchemaType> {
                    type
                };
            }
            else
            {
                types = EnumUtils.GetFlagsValues(type).Where(v => v != JSchemaType.None).ToList();
            }

            if (types.Count == 0)
            {
                return;
            }

            writer.WritePropertyName(propertyName);

            if (types.Count == 1)
            {
                writer.WriteValue(JSchemaTypeHelpers.MapType(types[0]));
                return;
            }

            writer.WriteStartArray();
            foreach (JSchemaType schemaType in types)
            {
                writer.WriteValue(JSchemaTypeHelpers.MapType(schemaType));
            }
            writer.WriteEndArray();
        }
Пример #2
0
        private void WriteType(string propertyName, JsonWriter writer, JSchemaType type)
        {
            if (Enum.IsDefined(typeof(JSchemaType), type))
            {
                writer.WritePropertyName(propertyName);
                writer.WriteValue(JSchemaTypeHelpers.MapType(type));
            }
            else
            {
                // Known to not need disposing.
                IEnumerator <JSchemaType> en = EnumUtils.GetFlagsValues(type).Where(v => v != JSchemaType.None).GetEnumerator();
                if (!en.MoveNext())
                {
                    return;
                }

                writer.WritePropertyName(propertyName);

                JSchemaType first = en.Current;

                if (en.MoveNext())
                {
                    writer.WriteStartArray();
                    writer.WriteValue(JSchemaTypeHelpers.MapType(first));
                    do
                    {
                        writer.WriteValue(JSchemaTypeHelpers.MapType(en.Current));
                    } while (en.MoveNext());

                    writer.WriteEndArray();
                }
                else
                {
                    writer.WriteValue(JSchemaTypeHelpers.MapType(first));
                }
            }
        }
Пример #3
0
        private object ReadType(JsonReader reader, string name)
        {
            EnsureRead(reader, name);

            List <JSchemaType> types       = new List <JSchemaType>();
            List <JSchema>     typeSchemas = null;

            switch (reader.TokenType)
            {
            case JsonToken.String:
                return(JSchemaTypeHelpers.MapType((string)reader.Value));

            case JsonToken.StartArray:
                while (reader.Read())
                {
                    switch (reader.TokenType)
                    {
                    case JsonToken.String:
                        JSchemaType t = JSchemaTypeHelpers.MapType((string)reader.Value);
                        if (typeSchemas != null)
                        {
                            typeSchemas.Add(new JSchema {
                                Type = t
                            });
                        }
                        else
                        {
                            types.Add(t);
                        }
                        break;

                    case JsonToken.StartObject:
                        if (typeSchemas == null)
                        {
                            typeSchemas = new List <JSchema>();
                            foreach (JSchemaType type in types)
                            {
                                typeSchemas.Add(new JSchema
                                {
                                    Type = type
                                });
                            }
                            types = null;
                        }
                        int            count = typeSchemas.Count;
                        List <JSchema> l     = typeSchemas;
                        LoadAndSetSchema(reader, s => SetAtIndex(l, count, s));
                        break;

                    case JsonToken.Comment:
                        // nom nom nom
                        break;

                    case JsonToken.EndArray:
                        if (typeSchemas != null)
                        {
                            return(typeSchemas);
                        }

                        JSchemaType finalType = JSchemaType.None;
                        foreach (JSchemaType type in types)
                        {
                            finalType = finalType | type;
                        }

                        return(finalType);

                    default:
                        throw JsonException.Create(reader as IJsonLineInfo, reader.Path, "Expected string token for type, got {0}.".FormatWith(CultureInfo.InvariantCulture, reader.TokenType));
                    }
                }
                break;

            default:
                throw JsonException.Create(null, reader.Path, "Expected array or string for '{0}', got {1}.".FormatWith(CultureInfo.InvariantCulture, Constants.PropertyNames.Type, reader.TokenType));
            }

            throw JsonReaderException.Create(reader, "Unexpected end when reading schema type.");
        }