本文整理汇总了C#中JsonToken类的典型用法代码示例。如果您正苦于以下问题:C# JsonToken类的具体用法?C# JsonToken怎么用?C# JsonToken使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsonToken类属于命名空间,在下文中一共展示了JsonToken类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AssertRead
protected void AssertRead(JsonToken expectedToken)
{
Reader.Read();
if (Reader.TokenType != expectedToken)
throw new Exception("Expected " + expectedToken + " but got " + Reader.TokenType);
}
示例2: EnsureEnum
protected void EnsureEnum(JsonToken token, object value)
{
if (Schema._enum != null && Schema._enum.Count > 0)
{
if (JsonTokenHelpers.IsPrimitiveOrStartToken(token))
{
if (Context.TokenWriter == null)
{
Context.TokenWriter = new JTokenWriter();
Context.TokenWriter.WriteToken(token, value);
}
}
if (JsonTokenHelpers.IsPrimitiveOrEndToken(token))
{
if (!Schema._enum.ContainsValue(Context.TokenWriter.CurrentToken, JToken.EqualityComparer))
{
StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
Context.TokenWriter.CurrentToken.WriteTo(new JsonTextWriter(sw));
RaiseError($"Value {sw.ToString()} is not defined in enum.", ErrorType.Enum, Schema, value, null);
}
}
}
}
示例3: EvaluateToken
public void EvaluateToken(JsonToken token, object value, int depth)
{
if (EvaluateTokenCore(token, value, depth))
{
Complete = true;
}
}
示例4: JsonSerializer
private JsonSerializer bareserializer = new JsonSerializer(); // full default settings, separate context
private static void ReadExpectType(JsonReader reader, JsonToken expected)
{
if (!reader.Read())
throw new InvalidOperationException();
if (reader.TokenType != expected)
throw new InvalidOperationException();
}
示例5: EvaluateTokenCore
protected override bool EvaluateTokenCore(JsonToken token, object value, int depth)
{
if (depth == InitialDepth && JsonTokenHelpers.IsPrimitiveOrEndToken(token))
{
int validCount = GetChildren().Count(IsValidPredicate);
if (validCount != 1)
{
List<int> validIndexes = new List<int>();
int index = 0;
foreach (SchemaScope schemaScope in GetChildren())
{
if (schemaScope.IsValid)
validIndexes.Add(index);
index++;
}
string message = "JSON is valid against more than one schema from 'oneOf'. ";
if (validIndexes.Count > 0)
message += "Valid schema indexes: {0}.".FormatWith(CultureInfo.InvariantCulture, StringHelpers.Join(", ", validIndexes));
else
message += "No valid schemas.";
RaiseError(message, ErrorType.OneOf, ParentSchemaScope.Schema, null, ConditionalContext.Errors);
}
return true;
}
return false;
}
示例6: Read
public override bool Read()
{
LBTOK l = (LBTOK)r.ReadByte();
switch (l)
{
case LBTOK.StartArray: t = JsonToken.StartArray; v = null; break;
case LBTOK.EndArray: t = JsonToken.EndArray; v = null; break;
case LBTOK.StartObject: t = JsonToken.StartObject; v = null; break;
case LBTOK.EndObject: t = JsonToken.EndObject; v = null; break;
case LBTOK.Null: t = JsonToken.Null; v = null; break;
case LBTOK.False: t = JsonToken.Boolean; v = false; break;
case LBTOK.True: t = JsonToken.Boolean; v = true; break;
case LBTOK.Property: t = JsonToken.PropertyName; v = r.ReadString(); break;
case LBTOK.Undefined: t = JsonToken.Undefined; v = null; break;
case LBTOK.S8: t = JsonToken.Integer; v = r.ReadSByte(); break;
case LBTOK.U8: t = JsonToken.Integer; v = r.ReadByte(); break;
case LBTOK.S16: t = JsonToken.Integer; v = r.ReadInt16(); break;
case LBTOK.U16: t = JsonToken.Integer; v = r.ReadUInt16(); break;
case LBTOK.S32: t = JsonToken.Integer; v = r.ReadInt32(); break;
case LBTOK.U32: t = JsonToken.Integer; v = r.ReadUInt32(); break;
case LBTOK.S64: t = JsonToken.Integer; v = r.ReadInt64(); break;
case LBTOK.U64: t = JsonToken.Integer; v = r.ReadUInt64(); break;
case LBTOK.String: t = JsonToken.String; v = r.ReadString(); break;
case LBTOK.F32: t = JsonToken.Float; v = r.ReadSingle(); break;
case LBTOK.F64: t = JsonToken.Float; v = r.ReadDouble(); break;
case LBTOK.ByteArray: t = JsonToken.Bytes; v = r.ReadBytes(r.ReadInt32()); break;
default:
throw new InvalidOperationException();
}
return true;
}
示例7: EvaluateTokenCore
protected override bool EvaluateTokenCore(JsonToken token, object value, int depth)
{
if (depth == InitialDepth && JsonTokenHelpers.IsPrimitiveOrEndToken(token))
{
if (!GetChildren().All(IsValidPredicate))
{
List<int> invalidIndexes = new List<int>();
int index = 0;
foreach (SchemaScope schemaScope in GetChildren())
{
if (!schemaScope.IsValid)
{
invalidIndexes.Add(index);
}
index++;
}
IFormattable message = $"JSON does not match all schemas from 'allOf'. Invalid schema indexes: {StringHelpers.Join(", ", invalidIndexes)}.";
RaiseError(message, ErrorType.AllOf, ParentSchemaScope.Schema, null, ConditionalContext.Errors);
}
return true;
}
return false;
}
示例8: ValidateCurrentToken
public void ValidateCurrentToken(JsonToken token, object value, int depth)
{
if (_scopes.Count == 0)
{
if (Schema == null)
throw new JSchemaException("No schema has been set for the validator.");
LicenseHelpers.IncrementAndCheckValidationCount();
SchemaScope.CreateTokenScope(token, Schema, _context, null, depth);
}
if (TokenWriter != null)
TokenWriter.WriteToken(token, value);
for (int i = _scopes.Count - 1; i >= 0; i--)
{
Scope scope = _scopes[i];
if (!scope.Complete)
scope.EvaluateToken(token, value, depth);
else
_scopes.RemoveAt(i);
}
if (TokenWriter != null && TokenWriter.Top == 0)
TokenWriter = null;
}
示例9: SkipJsonToken
protected static object SkipJsonToken(JsonReader reader, JsonToken type)
{
if (!reader.Read() || reader.TokenType != type)
throw new InvalidDataException(string.Format("Invalid JSON, expected \"{0}\", but got {1}, {2}", type, reader.TokenType, reader.Value));
return reader.Value;
}
示例10: InitializeScopes
public void InitializeScopes(JsonToken token, List<JSchema> schemas)
{
foreach (JSchema schema in schemas)
{
SchemaScope.CreateTokenScope(token, schema, ConditionalContext, this, InitialDepth);
}
}
示例11: PushBack
internal void PushBack(JsonToken token)
{
if (bufferedToken != null)
{
throw new InvalidOperationException("Can't push back twice");
}
bufferedToken = token;
}
示例12: WriteEnd
protected override void WriteEnd(JsonToken token)
{
base.WriteEnd(token);
this.RemoveParent();
if (this.Top != 0)
return;
this._writer.WriteToken(this._root);
}
示例13: ReadJsonProperty
protected static object ReadJsonProperty(JsonReader reader, string propertyname, JsonToken type)
{
var p = SkipJsonToken(reader, JsonToken.PropertyName);
if (p == null || p.ToString() != propertyname)
throw new InvalidDataException(string.Format("Invalid JSON, expected property \"{0}\", but got {1}, {2}", propertyname, reader.TokenType, reader.Value));
return SkipJsonToken(reader, type);
}
示例14: Read
static void Read(JsonReader jsonReader, JsonToken expected)
{
jsonReader.Read();
if (jsonReader.TokenType != expected)
{
throw new Exception(string.Format("expected {0}", expected));
}
}
示例15: ReadState
public ReadState(JsonToken type, object val = null)
{
TokenType = type;
if(type == JsonToken.String && val != null)
Value = val.ToString();
else
Value = val;
}