public void EmptyObjectJsonString() { // given var jsonObjectDict = new Dictionary <string, JsonValue>(); Assert.That(JsonObjectValue.FromDictionary(jsonObjectDict).ToString(), Is.EqualTo("{}")); }
public void ParsingSpecialUnicodeCharacter() { var jsonObjectDict = new Dictionary <string, JsonValue>(); jsonObjectDict.Add("Test", JsonStringValue.FromString("/\b\f\n\r\t\"\\\ud834\uDD1E")); Assert.That(JsonObjectValue.FromDictionary(jsonObjectDict).ToString(), Is.EqualTo("{\"Test\":\"\\/\\b\\f\\n\\r\\t\\\"\\\\𝄞\"}")); }
/// <summary> /// Parses the given token in init state. /// <para> /// This state is the state right after starting parsing the JSON string. /// Valid and expected tokens in this state are simple value tokens or start of a compound value. /// </para> /// </summary> /// <param name="token">the token to parse</param> /// <exception cref="JsonParserException">in case parsing fails.</exception> private void ParseInitState(JsonToken token) { EnsureTokenIsNotNull(token, "No JSON object could be decoded"); switch (token.TokenType) { case JsonTokenType.LITERAL_NULL: // fallthrough case JsonTokenType.LITERAL_BOOLEAN: // fallthrough case JsonTokenType.VALUE_STRING: // fallthrough case JsonTokenType.VALUE_NUMBER: // fallthrough valueContainerStack.AddFirst(new JsonValueContainer(TokenToSimpleJsonValue(token))); state = JsonParserState.END; break; case JsonTokenType.LEFT_SQUARE_BRACKET: var jsonValueList = new LinkedList <JsonValue>(); valueContainerStack.AddFirst(new JsonValueContainer(JsonArrayValue.FromList(jsonValueList), jsonValueList)); state = JsonParserState.IN_ARRAY_START; break; case JsonTokenType.LEFT_BRACE: var jsonObjectDict = new Dictionary <string, JsonValue>(); valueContainerStack.AddFirst(new JsonValueContainer(JsonObjectValue.FromDictionary(jsonObjectDict), jsonObjectDict)); state = JsonParserState.IN_OBJECT_START; break; default: state = JsonParserState.ERROR; throw new JsonParserException(UnexpectedTokenErrorMessage(token, "at start of input")); } }
public void IsObjectType() { // given var emptyDict = new Dictionary <string, JsonValue>(); // then Assert.That(JsonObjectValue.FromDictionary(emptyDict).ValueType, Is.EqualTo(JsonValueType.OBJECT)); }
private static void ApplyApplicationId(ResponseAttributes.Builder builder, JsonObjectValue appConfigObject) { if (!(appConfigObject[ResponseKeyApplicationId] is JsonStringValue stringValue)) { return; } builder.WithApplicationId(stringValue.Value); }
/// <summary> /// Utility method to parse the start of a nested object. /// <para> /// This method is called when the left brace token is encountered. /// </para> /// </summary> private void ParseStartOfNestedObject() { stateStack.AddFirst(JsonParserState.IN_ARRAY_VALUE); var jsonObjectDict = new Dictionary <string, JsonValue>(); valueContainerStack.AddFirst(new JsonValueContainer(JsonObjectValue.FromDictionary(jsonObjectDict), jsonObjectDict)); state = JsonParserState.IN_OBJECT_START; }
public string Build() { if (overriddenKeys.Count > 0) { AddNonOverridableAttribute("dt.overridden_keys", JsonArrayValue.FromList(overriddenKeys)); } return(JsonObjectValue.FromDictionary(attributes).ToString()); }
public void SingleElementObjectJsonString() { // given var jsonObjectDict = new Dictionary <string, JsonValue>(); jsonObjectDict.Add("Test", JsonBooleanValue.FromValue(false)); Assert.That(JsonObjectValue.FromDictionary(jsonObjectDict).ToString(), Is.EqualTo("{\"Test\":false}")); }
private static void ApplyStatus(ResponseAttributes.Builder builder, JsonObjectValue dynConfigObject) { if (!(dynConfigObject[ResponseKeyStatus] is JsonStringValue stringValue)) { return; } builder.WithStatus(stringValue.Value); }
static Option <IJsonValue> TryParseObject(string name, JToken token) { var result = none <IJsonValue>(); if (token.Type == JTokenType.Object) { result = new JsonObjectValue(name, ParseChildren(token)); } return(result); }
private bool ValidateObject(object[] expected, JsonObjectValue actual) { if (expected.Length / 2 != actual.Count) { return(false); } for (var i = 0; i < expected.Length / 2; i++) { var k = expected[i * 2] as string; var v = expected[i * 2 + 1]; if (!actual.ContainsKey(k)) { return(false); } var a = actual[k]; switch (a) { case JsonArrayValue actualArray: if (v is not object[] expectedArray) { return(false); } if (!this.ValidateArray(expectedArray, actualArray)) { return(false); } continue; case JsonObjectValue actualObject: if (v is not object[] expectedObject) { return(false); } if (!this.ValidateObject(expectedObject, actualObject)) { return(false); } continue; } if (!a.Equals(v)) { return(false); } } return(true); }
private static void ApplyRootAttributes(ResponseAttributes.Builder builder, JsonObjectValue rootObject) { if (!(rootObject[ResponseKeyTimestampInMillis] is JsonNumberValue numberValue)) { return; } var timestamp = numberValue.LongValue; builder.WithTimestampInMilliseconds(timestamp); }
private static void ApplyCapture(ResponseAttributes.Builder builder, JsonObjectValue appConfigObject) { if (!(appConfigObject[ResponseKeyCapture] is JsonNumberValue numberValue)) { return; } var capture = numberValue.IntValue; builder.WithCapture(capture == 1); }
private static void ApplyReportErrors(ResponseAttributes.Builder builder, JsonObjectValue appConfigObject) { if (!(appConfigObject[ResponseKeyReportErrors] is JsonNumberValue numberValue)) { return; } var reportErrors = numberValue.IntValue; builder.WithCaptureErrors(reportErrors != 0); }
private static void ApplyMultiplicity(ResponseAttributes.Builder builder, JsonObjectValue dynConfigObject) { if (!(dynConfigObject[ResponseKeyMultiplicity] is JsonNumberValue numberValue)) { return; } var multiplicity = numberValue.IntValue; builder.WithMultiplicity(multiplicity); }
private static void ApplyBeaconSizeInKb(ResponseAttributes.Builder builder, JsonObjectValue agentConfigObject) { if (!(agentConfigObject[ResponseKeyMaxBeaconSizeInKb] is JsonNumberValue numberValue)) { return; } var beaconSizeInKb = numberValue.IntValue; builder.WithMaxBeaconSizeInBytes(beaconSizeInKb * 1024); }
private static void ApplyServerId(ResponseAttributes.Builder builder, JsonObjectValue dynConfigObject) { if (!(dynConfigObject[ResponseKeyServerId] is JsonNumberValue numberValue)) { return; } var serverId = numberValue.IntValue; builder.WithServerId(serverId); }
private static void ApplySendIntervalInSec(ResponseAttributes.Builder builder, JsonObjectValue agentConfigObject) { if (!(agentConfigObject[ResponseKeySendIntervalInSec] is JsonNumberValue numberValue)) { return; } var intervalInMillis = (int)TimeSpan.FromSeconds(numberValue.IntValue).TotalMilliseconds; builder.WithSendIntervalInMilliseconds(intervalInMillis); }
private static void ApplyMaxEventsPerSession(ResponseAttributes.Builder builder, JsonObjectValue agentConfigObject) { if (!(agentConfigObject[ResponseKeyMaxEventsPerSession] is JsonNumberValue numberValue)) { return; } var maxEvents = numberValue.IntValue; builder.WithMaxEventsPerSession(maxEvents); }
private static void ApplyMaxSessionDurationInMin(ResponseAttributes.Builder builder, JsonObjectValue agentConfigObject) { if (!(agentConfigObject[ResponseKeyMaxSessionDurationInMin] is JsonNumberValue numberValue)) { return; } var sessionDurationMillis = (int)TimeSpan.FromMinutes(numberValue.IntValue).TotalMilliseconds; builder.WithMaxSessionDurationInMilliseconds(sessionDurationMillis); }
private static void ApplyVisitStoreVersion(ResponseAttributes.Builder builder, JsonObjectValue agentConfigObject) { if (!(agentConfigObject[ResponseKeyVisitStoreVersion] is JsonNumberValue numberValue)) { return; } var visitStoreVersion = numberValue.IntValue; builder.WithVisitStoreVersion(visitStoreVersion); }
private static void ApplyApplicationConfiguration(ResponseAttributes.Builder builder, JsonObjectValue rootObject) { if (!(rootObject[ResponseKeyAppConfig] is JsonObjectValue appConfigObject)) { return; } ApplyCapture(builder, appConfigObject); ApplyReportCrashes(builder, appConfigObject); ApplyReportErrors(builder, appConfigObject); ApplyApplicationId(builder, appConfigObject); }
public void IndexerReturnsNullIfKeyDoesNotExist() { // given var jsonObjectDict = new Dictionary <string, JsonValue>(); var target = JsonObjectValue.FromDictionary(jsonObjectDict); // when var obtained = target["foo"]; // then Assert.That(obtained, Is.Null); }
public void CountDelegatesToUnderlyingDictionary() { // given var jsonObjectDict = Substitute.For <IDictionary <string, JsonValue> >(); jsonObjectDict.Count.Returns(42); var target = JsonObjectValue.FromDictionary(jsonObjectDict); // when var obtained = target.Count; // then Assert.That(obtained, Is.EqualTo(42)); _ = jsonObjectDict.Received(1).Count; }
public void NestedObjectInObjectJsonString() { // given var jsonObjectDict = new Dictionary <string, JsonValue>(); jsonObjectDict.Add("Test", JsonBooleanValue.FromValue(false)); var jsonNestedObjectDict = new Dictionary <string, JsonValue>(); jsonNestedObjectDict.Add("Test3", JsonNumberValue.FromLong(1)); jsonObjectDict.Add("Test2", JsonObjectValue.FromDictionary(jsonNestedObjectDict)); Assert.That(JsonObjectValue.FromDictionary(jsonObjectDict).ToString(), Is.EqualTo("{\"Test\":false,\"Test2\":{\"Test3\":1}}")); }
public void KeysDelegatesToUnderlyingDictionary() { //given var jsonObjectDict = Substitute.For <IDictionary <string, JsonValue> >(); ICollection <string> keys = new HashSet <string> { "foobar" }; jsonObjectDict.Keys.Returns(keys); var target = JsonObjectValue.FromDictionary(jsonObjectDict); // when var obtained = target.Keys; // then Assert.That(obtained, Is.Not.Null); _ = jsonObjectDict.Received(1).Keys; }
/// <summary> /// Parses a token right after a JSON key-value delimiter (":") was parsed. /// </summary> /// <param name="token">the token to parse.</param> /// <exception cref="JsonParserException">in case parsing fails</exception> private void ParseInObjectColonState(JsonToken token) { EnsureTokenIsNotNull(token, UNTERMINATED_JSON_OBJECT_ERROR); EnsureTopLevelElementIsAJsonObject(); switch (token.TokenType) { case JsonTokenType.VALUE_NUMBER: // fallthrough case JsonTokenType.VALUE_STRING: // fallthrough case JsonTokenType.LITERAL_BOOLEAN: // fallthrough case JsonTokenType.LITERAL_NULL: // fallthrough // simple JSON value as object value valueContainerStack.First.Value.LastParsedObjectValue = TokenToSimpleJsonValue(token); state = JsonParserState.IN_OBJECT_VALUE; break; case JsonTokenType.LEFT_BRACE: // value is an object var jsonObjectDict = new Dictionary <string, JsonValue>(); valueContainerStack.AddFirst(new JsonValueContainer(JsonObjectValue.FromDictionary(jsonObjectDict), jsonObjectDict)); stateStack.AddFirst(JsonParserState.IN_OBJECT_VALUE); state = JsonParserState.IN_OBJECT_START; break; case JsonTokenType.LEFT_SQUARE_BRACKET: // value is an array var jsonValueList = new LinkedList <JsonValue>(); valueContainerStack.AddFirst(new JsonValueContainer(JsonArrayValue.FromList(jsonValueList), jsonValueList)); stateStack.AddFirst(JsonParserState.IN_OBJECT_VALUE); state = JsonParserState.IN_ARRAY_START; break; default: // any other token throw new JsonParserException( UnexpectedTokenErrorMessage(token, "after key-value pair encountered")); } }
public void ContainsDelegatesToUnderlyingDictionary() { // given var jsonObjectDict = Substitute.For <IDictionary <string, JsonValue> >(); jsonObjectDict.ContainsKey(Arg.Any <string>()).Returns(true); var target = JsonObjectValue.FromDictionary(jsonObjectDict); // when var obtained = target.ContainsKey("foo"); // then Assert.That(obtained, Is.True); _ = jsonObjectDict.Received(1).ContainsKey("foo"); // and when obtained = target.ContainsKey("bar"); // then Assert.That(obtained, Is.True); _ = jsonObjectDict.Received(1).ContainsKey("bar"); }
public void IndexerDelegatesToUnderlyingDictionary() { // given var valueOne = Substitute.For <JsonValue>(); var valueTwo = Substitute.For <JsonValue>(); var jsonObjectDict = new Dictionary <string, JsonValue> { ["foo"] = valueOne, ["bar"] = valueTwo }; var target = JsonObjectValue.FromDictionary(jsonObjectDict); // when var obtained = target["foo"]; // then Assert.That(obtained, Is.Not.Null.And.SameAs(valueOne)); // and when obtained = target["bar"]; // then Assert.That(obtained, Is.Not.Null.And.SameAs(valueTwo)); }
public void FromDictionaryReturnsNullIfArgumentIsNull() { // when constructed with null Assert.That(JsonObjectValue.FromDictionary(null), Is.Null); }