/// <summary> /// Read in a sequence of characters that are all valid in JSON numbers. Does /// not do a complete regex check to validate that this is actually a number. /// </summary> private async ValueTask <string> ReadJsonNumericCharsAsync(CancellationToken cancellationToken) { var strbld = new StringBuilder(); while (true) { //TODO: workaround for primitive types with TJsonProtocol, think - how to rewrite into more easy form without exceptions try { var ch = await Reader.PeekAsync(cancellationToken); if (!TJSONProtocolHelper.IsJsonNumeric(ch)) { break; } var c = (char)await Reader.ReadAsync(cancellationToken); strbld.Append(c); } catch (TTransportException) { break; } } return(strbld.ToString()); }
public override async Task WriteMapBeginAsync(TMap map, CancellationToken cancellationToken) { await WriteJsonArrayStartAsync(cancellationToken); await WriteJsonStringAsync(TJSONProtocolHelper.GetTypeNameForTypeId(map.KeyType), cancellationToken); await WriteJsonStringAsync(TJSONProtocolHelper.GetTypeNameForTypeId(map.ValueType), cancellationToken); await WriteJsonIntegerAsync(map.Count, cancellationToken); await WriteJsonObjectStartAsync(cancellationToken); }
public override async ValueTask <TList> ReadListBeginAsync(CancellationToken cancellationToken) { var list = new TList(); await ReadJsonArrayStartAsync(cancellationToken); list.ElementType = TJSONProtocolHelper.GetTypeIdForTypeName(await ReadJsonStringAsync(false, cancellationToken)); list.Count = (int) await ReadJsonIntegerAsync(cancellationToken); return(list); }
public override async Task <TSet> ReadSetBeginAsync(CancellationToken cancellationToken) { var set = new TSet(); await ReadJsonArrayStartAsync(cancellationToken); set.ElementType = TJSONProtocolHelper.GetTypeIdForTypeName(await ReadJsonStringAsync(false, cancellationToken)); set.Count = (int) await ReadJsonIntegerAsync(cancellationToken); return(set); }
public override async Task <TMap> ReadMapBeginAsync(CancellationToken cancellationToken) { var map = new TMap(); await ReadJsonArrayStartAsync(cancellationToken); map.KeyType = TJSONProtocolHelper.GetTypeIdForTypeName(await ReadJsonStringAsync(false, cancellationToken)); map.ValueType = TJSONProtocolHelper.GetTypeIdForTypeName(await ReadJsonStringAsync(false, cancellationToken)); map.Count = (int) await ReadJsonIntegerAsync(cancellationToken); await ReadJsonObjectStartAsync(cancellationToken); return(map); }
public void ToHexVal_Test() { // input/output var chars = "0123456789abcdef"; var expectedHexValues = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; var sets = chars.Select((ch, i) => new Tuple <char, byte>(ch, expectedHexValues[i])).ToList(); foreach (var t in sets) { var actualResult = TJSONProtocolHelper.ToHexVal((byte)t.Item1); Assert.IsTrue(actualResult == t.Item2, $"Wrong mapping of char byte {t.Item1} to it expected hex value: {t.Item2}. Actual hex value: {actualResult}"); } }
public void ToHexChar_Test() { // input/output var hexValues = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; var expectedChars = "0123456789abcdef"; var sets = hexValues.Select((hv, i) => new Tuple <byte, char>(hv, expectedChars[i])).ToList(); foreach (var t in sets) { var actualResult = TJSONProtocolHelper.ToHexChar(t.Item1); Assert.IsTrue(actualResult == t.Item2, $"Wrong mapping of hex value {t.Item1} to it expected char: {t.Item2}. Actual hex value: {actualResult}"); } }
public void IsJsonNumeric_Test() { // input/output var correctJsonNumeric = "+-.0123456789Ee"; var incorrectJsonNumeric = "AaBcDd/*\\"; var sets = correctJsonNumeric.Select(ch => new Tuple <byte, bool>((byte)ch, true)).ToList(); sets.AddRange(incorrectJsonNumeric.Select(ch => new Tuple <byte, bool>((byte)ch, false))); foreach (var t in sets) { Assert.IsTrue(TJSONProtocolHelper.IsJsonNumeric(t.Item1) == t.Item2, $"Wrong mapping of Char {t.Item1} to bool: {t.Item2}"); } }
/// <summary> /// Write the bytes in array buf as a JSON characters, escaping as needed /// </summary> private async Task WriteJsonStringAsync(byte[] bytes, CancellationToken cancellationToken) { await Context.WriteConditionalDelimiterAsync(cancellationToken); await Trans.WriteAsync(TJSONProtocolConstants.Quote, cancellationToken); var len = bytes.Length; for (var i = 0; i < len; i++) { if ((bytes[i] & 0x00FF) >= 0x30) { if (bytes[i] == TJSONProtocolConstants.Backslash[0]) { await Trans.WriteAsync(TJSONProtocolConstants.Backslash, cancellationToken); await Trans.WriteAsync(TJSONProtocolConstants.Backslash, cancellationToken); } else { await Trans.WriteAsync(bytes, i, 1, cancellationToken); } } else { _tempBuffer[0] = TJSONProtocolConstants.JsonCharTable[bytes[i]]; if (_tempBuffer[0] == 1) { await Trans.WriteAsync(bytes, i, 1, cancellationToken); } else if (_tempBuffer[0] > 1) { await Trans.WriteAsync(TJSONProtocolConstants.Backslash, cancellationToken); await Trans.WriteAsync(_tempBuffer, 0, 1, cancellationToken); } else { await Trans.WriteAsync(TJSONProtocolConstants.EscSequences, cancellationToken); _tempBuffer[0] = TJSONProtocolHelper.ToHexChar((byte)(bytes[i] >> 4)); _tempBuffer[1] = TJSONProtocolHelper.ToHexChar(bytes[i]); await Trans.WriteAsync(_tempBuffer, 0, 2, cancellationToken); } } } await Trans.WriteAsync(TJSONProtocolConstants.Quote, cancellationToken); }
public override async Task <TField> ReadFieldBeginAsync(CancellationToken cancellationToken) { var field = new TField(); var ch = await Reader.PeekAsync(cancellationToken); if (ch == TJSONProtocolConstants.RightBrace[0]) { field.Type = TType.Stop; } else { field.ID = (short) await ReadJsonIntegerAsync(cancellationToken); await ReadJsonObjectStartAsync(cancellationToken); field.Type = TJSONProtocolHelper.GetTypeIdForTypeName(await ReadJsonStringAsync(false, cancellationToken)); } return(field); }
public void GetTypeIdForTypeName_Test() { // input/output var sets = new List <Tuple <TType, byte[]> > { new Tuple <TType, byte[]>(TType.Bool, TJSONProtocolConstants.TypeNames.NameBool), new Tuple <TType, byte[]>(TType.Byte, TJSONProtocolConstants.TypeNames.NameByte), new Tuple <TType, byte[]>(TType.I16, TJSONProtocolConstants.TypeNames.NameI16), new Tuple <TType, byte[]>(TType.I32, TJSONProtocolConstants.TypeNames.NameI32), new Tuple <TType, byte[]>(TType.I64, TJSONProtocolConstants.TypeNames.NameI64), new Tuple <TType, byte[]>(TType.Double, TJSONProtocolConstants.TypeNames.NameDouble), new Tuple <TType, byte[]>(TType.String, TJSONProtocolConstants.TypeNames.NameString), new Tuple <TType, byte[]>(TType.Struct, TJSONProtocolConstants.TypeNames.NameStruct), new Tuple <TType, byte[]>(TType.Map, TJSONProtocolConstants.TypeNames.NameMap), new Tuple <TType, byte[]>(TType.Set, TJSONProtocolConstants.TypeNames.NameSet), new Tuple <TType, byte[]>(TType.List, TJSONProtocolConstants.TypeNames.NameList), }; foreach (var t in sets) { Assert.IsTrue(TJSONProtocolHelper.GetTypeIdForTypeName(t.Item2) == t.Item1, $"Wrong mapping of TypeName {t.Item2} to TType: {t.Item1}"); } }
public override async Task WriteSetBeginAsync(TSet set, CancellationToken cancellationToken) { await WriteJsonArrayStartAsync(cancellationToken); await WriteJsonStringAsync(TJSONProtocolHelper.GetTypeNameForTypeId(set.ElementType), cancellationToken); await WriteJsonIntegerAsync(set.Count, cancellationToken); }
public void ToHexVal_WrongInputChar_Test() { TJSONProtocolHelper.ToHexVal((byte)'s'); }
public void GetTypeIdForTypeName_EmptyName_Test() { TJSONProtocolHelper.GetTypeIdForTypeName(new byte[] {}); }
public void GetTypeIdForTypeName_NonExistingTypeName_Test() { TJSONProtocolHelper.GetTypeIdForTypeName(new byte[] { 100 }); }
public void GetTypeNameForTypeId_TStop_Test() { TJSONProtocolHelper.GetTypeNameForTypeId(TType.Stop); }
public override async Task WriteFieldBeginAsync(TField field, CancellationToken cancellationToken) { await WriteJsonIntegerAsync(field.ID, cancellationToken); await WriteJsonObjectStartAsync(cancellationToken); await WriteJsonStringAsync(TJSONProtocolHelper.GetTypeNameForTypeId(field.Type), cancellationToken); }
public void GetTypeNameForTypeId_NonExistingTType_Test() { TJSONProtocolHelper.GetTypeNameForTypeId((TType)100); }
/// <summary> /// Read in a JSON string, unescaping as appropriate.. Skip Reading from the /// context if skipContext is true. /// </summary> private async ValueTask <byte[]> ReadJsonStringAsync(bool skipContext, CancellationToken cancellationToken) { using (var buffer = new MemoryStream()) { var codeunits = new List <char>(); if (!skipContext) { await Context.ReadConditionalDelimiterAsync(cancellationToken); } await ReadJsonSyntaxCharAsync(TJSONProtocolConstants.Quote, cancellationToken); while (true) { var ch = await Reader.ReadAsync(cancellationToken); if (ch == TJSONProtocolConstants.Quote[0]) { break; } // escaped? if (ch != TJSONProtocolConstants.EscSequences[0]) { await buffer.WriteAsync(new[] { ch }, 0, 1, cancellationToken); continue; } // distinguish between \uXXXX and \? ch = await Reader.ReadAsync(cancellationToken); if (ch != TJSONProtocolConstants.EscSequences[1]) // control chars like \n { var off = Array.IndexOf(TJSONProtocolConstants.EscapeChars, (char)ch); if (off == -1) { throw new TProtocolException(TProtocolException.INVALID_DATA, "Expected control char"); } ch = TJSONProtocolConstants.EscapeCharValues[off]; await buffer.WriteAsync(new[] { ch }, 0, 1, cancellationToken); continue; } // it's \uXXXX await Trans.ReadAllAsync(_tempBuffer, 0, 4, cancellationToken); var wch = (short)((TJSONProtocolHelper.ToHexVal(_tempBuffer[0]) << 12) + (TJSONProtocolHelper.ToHexVal(_tempBuffer[1]) << 8) + (TJSONProtocolHelper.ToHexVal(_tempBuffer[2]) << 4) + TJSONProtocolHelper.ToHexVal(_tempBuffer[3])); if (char.IsHighSurrogate((char)wch)) { if (codeunits.Count > 0) { throw new TProtocolException(TProtocolException.INVALID_DATA, "Expected low surrogate char"); } codeunits.Add((char)wch); } else if (char.IsLowSurrogate((char)wch)) { if (codeunits.Count == 0) { throw new TProtocolException(TProtocolException.INVALID_DATA, "Expected high surrogate char"); } codeunits.Add((char)wch); var tmp = Utf8Encoding.GetBytes(codeunits.ToArray()); await buffer.WriteAsync(tmp, 0, tmp.Length, cancellationToken); codeunits.Clear(); } else { var tmp = Utf8Encoding.GetBytes(new[] { (char)wch }); await buffer.WriteAsync(tmp, 0, tmp.Length, cancellationToken); } } if (codeunits.Count > 0) { throw new TProtocolException(TProtocolException.INVALID_DATA, "Expected low surrogate char"); } return(buffer.ToArray()); } }
public void GetTypeIdForTypeName_TStopTypeName_Test() { TJSONProtocolHelper.GetTypeIdForTypeName(new [] { (byte)TType.Stop, (byte)TType.Stop }); }
public override async Task WriteListBeginAsync(TList list, CancellationToken cancellationToken) { await WriteJsonArrayStartAsync(cancellationToken).ConfigureAwait(false); await WriteJsonStringAsync(TJSONProtocolHelper.GetTypeNameForTypeId(list.ElementType), cancellationToken).ConfigureAwait(false); await WriteJsonIntegerAsync(list.Count, cancellationToken).ConfigureAwait(false); }