private void WriteMemberInternal(string name, string value, bool quoteValue) { JSonWriterTokenInfo currentToken = VerifyTopTokenEqualTo(JSonWriterTokenType.Object); if (currentToken != null) { currentToken.AddItem(_output, Indent, CompactEnumerables); } _output.Write('"'); StringHelper.WriteSecureString(_output, name); _output.Write('"'); if (CompactEnumerables) { _output.Write(':'); } else { _output.Write(": "); } if (quoteValue) { _output.Write('"'); } _output.Write(value); if (quoteValue) { _output.Write('"'); } }
/// <summary> /// Writes an array start. /// </summary> public void WriteArrayBegin() { JSonWriterTokenInfo currentToken = _tokens.Count > 0 ? _tokens.Peek() : null; _tokens.Push(new JSonWriterTokenInfo(JSonWriterTokenType.Array, _level++)); if (currentToken != null) { currentToken.AddItem(_output, currentToken.TokenType != JSonWriterTokenType.MemberValue && Indent, CompactEnumerables); } _output.Write('['); }
/// <summary> /// Writes an object start. /// </summary> public void WriteObjectBegin() { // object can't be directly embedded inside another object: JSonWriterTokenInfo currentToken = VerifyTopTokenDifferentThan(JSonWriterTokenType.Object); _tokens.Push(new JSonWriterTokenInfo(JSonWriterTokenType.Object, _level++)); if (currentToken != null) { currentToken.AddItem(_output, currentToken.TokenType != JSonWriterTokenType.MemberValue && Indent, CompactEnumerables); } _output.Write('{'); }
/// <summary> /// Writes an array end. /// </summary> public void WriteArrayEnd() { JSonWriterTokenInfo currentToken = VerifyTopTokenEqualToAndDequeue(JSonWriterTokenType.Array); // if this array was added as a value in if (_tokens.Count > 0 && _tokens.Peek().TokenType == JSonWriterTokenType.MemberValue) { _tokens.Pop(); } if (currentToken != null) { currentToken.RemoveItem(_output, Indent); } _level--; _output.Write(']'); }
private JSonWriterTokenInfo VerifyTopTokenEqualToAndDequeue(JSonWriterTokenType expected) { JSonWriterTokenType currentToken = JSonWriterTokenType.Nothing; JSonWriterTokenInfo t = null; if (_tokens.Count > 0) { t = _tokens.Pop(); currentToken = t.TokenType; } if (currentToken != expected) { throw new JSonWriterException(expected, currentToken); } return(t); }
/// <summary> /// Writes a member only to an object. /// It requires to be called before writing an object (or array) inside current object. /// Writing values after call to this function is also allowed, however it is not recommented /// due to number of checks performed. The better performance can be achieved /// if other overloaded functions are used. /// </summary> public void WriteMember(string name) { JSonWriterTokenInfo currentToken = VerifyTopTokenEqualTo(JSonWriterTokenType.Object); currentToken.AddItem(_output, Indent, CompactEnumerables); _tokens.Push(new JSonWriterTokenInfo(JSonWriterTokenType.MemberValue, _level)); _output.Write('"'); StringHelper.WriteSecureString(_output, name); _output.Write('"'); if (CompactEnumerables) { _output.Write(':'); } else { _output.Write(": "); } }
private void WriteValueInternal(string value, bool quoteValue) { // allow adding value item, when it's just a simple writing without wrapping // in array or object: if (_tokens.Count == 0) { // push dummy item on tokens stack, so that on the next try, verification will fail, // only one value item can be added! _tokens.Push(new JSonWriterTokenInfo(JSonWriterTokenType.Nothing, -1)); if (quoteValue) { _output.Write('"'); } _output.Write(value); if (quoteValue) { _output.Write('"'); } return; } JSonWriterTokenInfo currentToken = VerifyTopTokenEqualTo(JSonWriterTokenType.Array, JSonWriterTokenType.MemberValue); currentToken.AddItem(_output, currentToken.TokenType != JSonWriterTokenType.MemberValue && Indent, CompactEnumerables); if (currentToken.TokenType == JSonWriterTokenType.MemberValue) { _tokens.Pop(); } if (quoteValue) { _output.Write('"'); } _output.Write(value); if (quoteValue) { _output.Write('"'); } }