private void WriteToken(IList<JsonSchemaModel> schemas)
{
foreach (SchemaScope schemaScope in _stack)
{
bool isInUniqueArray = (schemaScope.TokenType == JTokenType.Array && schemaScope.IsUniqueArray && schemaScope.ArrayItemCount > 0);
if (isInUniqueArray || schemas.Any(s => s.Enum != null))
{
if (schemaScope.CurrentItemWriter == null)
{
if (JsonTokenUtils.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 (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);
}
}
}
}
}
}
}
}