protected override Token CacheText(FullToken fullToken) { Token token = fullToken.Token; switch (token.Type) { case TokenType.String: case TokenType.EncodedString: case TokenType.Number: Key tokenKey = fullToken.DecodedKey; if (!this.textBufferPositions.TryGetValue(tokenKey, out int start)) { start = this.textBuffer.Length; this.textBuffer.Append(tokenKey.Text); this.textBufferPositions.Add(tokenKey, start); } token = new Token(token.Type == TokenType.Number ? TokenType.Number : TokenType.String, start, tokenKey.Text.Length, tokenKey.Hash); break; default: token = new Token(token.Type, 0, 0, 0); break; } return(token); }
public static string EncodeToken(FullToken token) { using (StringWriter writer = new StringWriter(CultureInfo.InvariantCulture)) { EncodingUtility.EncodeToken(token, writer); return(writer.ToString()); } }
public ParsedItemizer(JsonValue value) { this.token = FullToken.Empty; this.states = new Stack <State>(Constants.BufferSize); this.state = new State() { value = value }; }
public ObjectItemizer(object value) { this.token = FullToken.Empty; this.typeCache = TypeCache.Instance; this.states = new Stack <State>(Constants.BufferSize); this.state = new State() { value = value, typeInfo = this.typeCache.GetTypeInfo(value?.GetType()), }; }
private MemberValue DeserializeValue(Item item, object parent, string key, MemberInfo memberInfo) { switch (item.Type) { default: Debug.Fail($"Unexpected item type: {item.Type}"); break; case ItemType.ArrayStart: case ItemType.ObjectStart: return(this.DeserializeObjectOrArray(item, parent, key, memberInfo)); case ItemType.Value: if (memberInfo != null) { FullToken token = this.itemizer.ValueToken; object value = null; switch (token.Type) { case TokenType.True: value = Constants.TrueObject; break; case TokenType.False: value = Constants.FalseObject; break; case TokenType.Number: value = (item.Data is decimal) ? item.Data : decimal.Parse((item.Data is string stringForDecimal) ? stringForDecimal : token.DecodedKey.Text); break; case TokenType.String: value = (item.Data is string stringForString) ? stringForString : token.DecodedKey.Text; break; case TokenType.EncodedString: value = token.DecodedKey.Text; break; } return(new MemberValue(memberInfo, key, value)); } break; } return(MemberValue.Invalid); }
protected JsonException ParseException(FullToken token, string message, params object[] args) { message = string.Format(CultureInfo.CurrentCulture, message, args); string tokenText = token.EncodedText; if (tokenText.Length > 32) { tokenText = tokenText.Substring(0, 32) + "..." + (tokenText[0] == '\"' ? "\"" : string.Empty); } return(new JsonException(token, string.Format(CultureInfo.CurrentCulture, Resources.Parser_Message, token.Line + 1, // {0} token.Column + 1, // {1} tokenText, // {2} this.CurrentJsonPath, // {3} message))); // {4} }
public static void EncodeTokenAsString(FullToken token, TextWriter writer) { if (!token.HasType(TokenType.AnyValue)) { throw JsonException.New(Resources.Convert_KeyStringFailed, token.Type); } if (!token.HasType(TokenType.AnyString)) { writer.Write('\"'); } EncodingUtility.EncodeToken(token.FullText, token.Token, writer); if (!token.HasType(TokenType.AnyString)) { writer.Write('\"'); } }
protected override void Parse(List <string> tokens) { NodeList = NodeList ?? new List <object>(); NodeList.Clear(); string token; while ((token = tokens.Shift()) != null) { Match fullTokenMatch = FullToken.Match(token); if (fullTokenMatch.Success && BlockDelimiter == fullTokenMatch.Groups[1].Value) { EndTag(); return; } else { NodeList.Add(token); } } AssertMissingDelimitation(); }
/// <summary> /// /// </summary> /// <param name="tokens"></param> protected override void Parse(IEnumerable <string> tokens) { NodeList.Clear(); string token; var t = tokens as List <string>; while ((token = t.Shift()) != null) { var fullTokenMatch = FullToken.Match(token); if (fullTokenMatch.Success && BlockDelimiter == fullTokenMatch.Groups[1].Value) { EndTag(); return; } else { NodeList.Add(token); } } AssertMissingDelimitation(); }
private void CreateTokenAndItem(object obj, out FullToken fullToken, out Item item) { Token token; string tokenText; switch (Convert.GetTypeCode(obj)) { case TypeCode.Boolean: { bool asBool = (bool)obj; tokenText = asBool ? "true" : "false"; token = asBool ? new Token(TokenType.True, 0, tokenText.Length, StringHasher.TrueHash) : new Token(TokenType.False, 0, tokenText.Length, StringHasher.FalseHash); item = new Item(ItemType.Value); } break; case TypeCode.Char: case TypeCode.String: case TypeCode.DateTime: tokenText = Convert.ToString(obj, CultureInfo.InvariantCulture); token = new Token(TokenType.String, 0, tokenText.Length, StringHasher.Hash(tokenText)); item = new Item(ItemType.Value, tokenText); break; case TypeCode.Byte: case TypeCode.Decimal: case TypeCode.Double: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: case TypeCode.SByte: case TypeCode.Single: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: tokenText = Convert.ToString(obj, CultureInfo.InvariantCulture); token = new Token(TokenType.Number, 0, tokenText.Length, StringHasher.Hash(tokenText)); item = new Item(ItemType.Value, Convert.ToDecimal(obj, CultureInfo.InvariantCulture)); break; case TypeCode.DBNull: case TypeCode.Empty: tokenText = "null"; token = new Token(TokenType.Null, 0, tokenText.Length, StringHasher.NullHash); item = new Item(ItemType.Value); break; default: case TypeCode.Object: { TypeInfo typeInfo = this.typeCache.GetTypeInfo(obj.GetType()); TokenType tokenType = typeInfo.IsArray ? (typeInfo.ElementTypeInfo.IsKeyValuePair ? TokenType.ObjectValue : TokenType.ArrayValue) : (typeInfo.SerializeAsObject ? TokenType.ObjectValue : TokenType.ArrayValue); tokenText = string.Empty; token = new Token(tokenType, 0, 0, StringHasher.NullHash); item = (tokenType == TokenType.ArrayValue) ? new Item(ItemType.ArrayStart, (obj is ICollection collection) ? collection.Count : 0) : new Item(ItemType.ObjectStart); } break; } fullToken = new FullToken(token, tokenText, 0, 0); }
internal JsonException(FullToken token, string message, Exception innerException = null) : base(message ?? innerException?.Message ?? string.Empty, innerException) { this.Token = token; }
public override Item NextItem() { Item item = default; JsonValue value = null; switch (this.state.type) { case StateType.RootValue: if (this.state.index == 0) { value = this.state.value; } else { item = new Item(ItemType.End); } break; case StateType.ArrayValue: if (this.state.index < this.state.value.InternalArray.Length) { value = this.state.value.InternalArray[this.state.index]; } else { item = new Item(ItemType.ArrayEnd); this.state = this.states.Pop(); } break; case StateType.ObjectValue: value = this.state.value.InternalArray[this.state.index]; this.state.type = StateType.ObjectKey; break; case StateType.ObjectKey: if (this.state.index < this.state.value.InternalKeys.Length) { item = new Item(ItemType.Key); this.token = this.state.value.InternalKeys[this.state.index].FullToken; this.state.type = StateType.ObjectValue; } else { item = new Item(ItemType.ObjectEnd); this.state = this.states.Pop(); } break; } if (value != null) { this.state.index++; switch (value.Type) { case TokenType.ArrayValue: case TokenType.ObjectValue: bool isArrayValue = (value.Type == TokenType.ArrayValue); item = new Item(isArrayValue ? ItemType.ArrayStart : ItemType.ObjectStart, value.InternalArray.Length); this.states.Push(this.state); this.state.value = value; this.state.index = 0; this.state.type = isArrayValue ? StateType.ArrayValue : StateType.ObjectKey; break; default: item = new Item(ItemType.Value, this.state.value.Value); this.token = value.FullToken; break; } } return(item); }
public override Item NextItem() { Item item = default; while (item.Type == ItemType.None) { this.token = this.NextToken(); switch (this.state) { case State.Value: switch (this.token.Type) { case TokenType.OpenBracket: this.openItems.Push(this.openItem); this.openItem = OpenItem.Array; item = new Item(ItemType.ArrayStart); break; case TokenType.CloseBracket: item = new Item(ItemType.ArrayEnd); this.openItem = this.openItems.Pop(); this.state = (this.openItem == OpenItem.None) ? State.None : State.Separator; break; case TokenType.OpenCurly: this.openItems.Push(this.openItem); this.openItem = OpenItem.Object; this.state = State.Key; item = new Item(ItemType.ObjectStart); break; default: if (this.token.HasType(TokenType.AnyValue)) { item = new Item(ItemType.Value); this.state = (this.openItem == OpenItem.None) ? State.None : State.Separator; } else { throw this.ParseException(this.token, Resources.Parser_ExpectedValue); } break; } break; case State.Key: switch (this.token.Type) { case TokenType.String: case TokenType.EncodedString: item = new Item(ItemType.Key); this.state = State.KeyColon; break; case TokenType.CloseCurly: item = new Item(ItemType.ObjectEnd); this.openItem = this.openItems.Pop(); this.state = (this.openItem == OpenItem.None) ? State.None : State.Separator; break; default: throw this.ParseException(this.token, Resources.Parser_ExpectedKeyName); } break; case State.KeyColon: switch (this.token.Type) { case TokenType.Colon: this.state = State.Value; break; default: throw this.ParseException(this.token, Resources.Parser_ExpectedKeyName); } break; case State.Separator: switch (this.openItem) { case OpenItem.Array: switch (this.token.Type) { case TokenType.Comma: this.state = State.Value; break; case TokenType.CloseBracket: item = new Item(ItemType.ArrayEnd); this.openItem = this.openItems.Pop(); this.state = (this.openItem == OpenItem.None) ? State.None : State.Separator; break; default: throw this.ParseException(this.token, Resources.Parser_ExpectedCommaOrBracket); } break; default: // must be OpenItem.Object switch (this.token.Type) { case TokenType.Comma: this.state = State.Key; break; case TokenType.CloseCurly: item = new Item(ItemType.ObjectEnd); this.openItem = this.openItems.Pop(); this.state = (this.openItem == OpenItem.None) ? State.None : State.Separator; break; default: throw this.ParseException(this.token, Resources.Parser_ExpectedCommaOrCurly); } break; } break; case State.None: switch (this.token.Type) { case TokenType.None: item = new Item(ItemType.End); break; default: throw this.ParseException(this.token, Resources.Parser_UnexpectedValue); } break; } } return(item); }
public static void EncodeToken(FullToken token, TextWriter writer) { EncodingUtility.EncodeToken(token.FullText, token.Token, writer); }