public static JsonObjectNode CreateIdentityCard(string name) { var card = new JsonObjectNode(); card["Name"] = new JsonStringNode(name); card["Age"] = new JsonIntegerNode(22); return(card); }
public void ToObject_Null() { // Arrange var stringNode = new JsonStringNode(); Type type = null; // Act stringNode.ConvertTo(type); }
public void ToObject_TrueToBoolean() { // Arrange var stringNode = new JsonStringNode("true"); // Act bool value = stringNode.ConvertTo <bool>(); // Assert Assert.AreEqual(true, value); }
public void ToObject_NumberStringToInt32() { // Arrange var stringNode = new JsonStringNode("42"); // Act int value = stringNode.ConvertTo <int>(); // Assert Assert.AreEqual(42, value); }
public void ToObject_NumberStringToString() { // Arrange var stringNode = new JsonStringNode("42"); // Act string value = stringNode.ConvertTo <string>(); // Assert Assert.AreEqual("42", value); }
public void ToString_EscapeSequences() { // Arrange var stringNode = new JsonStringNode("Hello\r\n\tWorld!"); // Act string result = stringNode.ToString(); // Assert Assert.AreEqual("\"Hello\\r\\n\\tWorld!\"", result); }
public void ToString_SimpleCharacters() { // Arrange var stringNode = new JsonStringNode("Hello World!"); // Act string result = stringNode.ToString(); // Assert Assert.AreEqual("\"Hello World!\"", result); }
/// <inheritdoc /> public override void Write(Utf8JsonWriter writer, DateTime?value, JsonSerializerOptions options) { if (value.HasValue) { writer.WriteStringValue(JsonStringNode.ToJson(value.Value, true)); } else { writer.WriteNullValue(); } }
/// <summary> /// Gets the YAML format string of the value. /// </summary> /// <returns>A YAML format string.</returns> public virtual string ToYamlString() { var str = new StringBuilder(); foreach (var prop in ListExtensions.ToGroups(this)) { if (prop.Key == null) { continue; } str.Append(prop.Key.IndexOfAny(StringExtensions.YamlSpecialChars) >= 0 ? JsonStringNode.ToJson(prop.Key) : prop.Key); str.Append(": "); var values = prop.ToList(); if (values == null || values.Count == 0) { str.AppendLine("!!null null"); continue; } if (values.Count == 1) { var item = values[0]; if (item == null) { str.AppendLine("!!null null"); continue; } str.AppendLine(item.Length == 0 || item.Length > 100 || item.IndexOfAny(StringExtensions.YamlSpecialChars) >= 0 ? JsonStringNode.ToJson(item) : item); continue; } foreach (var item in values) { str.AppendLine(); str.Append("- "); if (item == null) { str.AppendLine("!!null null"); continue; } str.AppendLine(item.IndexOfAny(StringExtensions.YamlSpecialChars) >= 0 ? JsonStringNode.ToJson(item) : item); } } return(str.ToString()); }
public void Value_set_SimpleCharacters() { // Arrange var stringNode = new JsonStringNode(); // Act stringNode.Value = "Hello World!"; // Assert Assert.AreEqual("Hello World!", stringNode.Value); }
public void ToString_Null() { // Arrange var stringNode = new JsonStringNode(null); // Act string result = stringNode.ToString(); // Assert Assert.AreEqual("\"\"", result); }
public void Value_set_Empty() { // Arrange var stringNode = new JsonStringNode(); // Act stringNode.Value = ""; // Assert Assert.AreEqual("", stringNode.Value); }
public void WriteTo_SimpleCharacters() { // Arrange var stringNode = new JsonStringNode("Hello World!"); var writer = JsonWriter.Create(); // Act stringNode.Write(writer); // Assert Assert.AreEqual("\"Hello World!\"", writer.ToString()); }
public void WriteTo_Empty() { // Arrange var stringNode = new JsonStringNode(""); var writer = JsonWriter.Create(); // Act stringNode.Write(writer); // Assert Assert.AreEqual("\"\"", writer.ToString()); }
public void Clone_SimpleCharacters() { // Arrange var stringNode = new JsonStringNode("Hello World!"); // Act var cloneNode = stringNode.Clone() as JsonStringNode; // Assert Assert.AreNotSame(stringNode, cloneNode); Assert.AreEqual(stringNode.Value, cloneNode.Value); }
public void Clone_Null() { // Arrange var stringNode = new JsonStringNode(null); // Act var cloneNode = stringNode.Clone() as JsonStringNode; // Assert Assert.AreNotSame(stringNode, cloneNode); Assert.AreEqual(stringNode.Value, cloneNode.Value); }
public void WriteTo_EscapeSequences() { // Arrange var stringNode = new JsonStringNode("Hello\r\n\tWorld!"); var writer = JsonWriter.Create(); // Act stringNode.Write(writer); // Assert Assert.AreEqual("\"Hello\\r\\n\\tWorld!\"", writer.ToString()); }
private JsonStringNode ProduceJsonString(JsonTokenizer tokenizer) { JsonStringNode jString = null; try { JsonToken t = tokenizer.Next(); string value = (string)t.Value; jString = (JsonStringNode)jsonTypeFactory.CreateString(value); } catch (Exception e) { throw new UnknownParserException("Failed to create a String node.", e, GetTailCharStream(tokenizer), PeekCharStream(tokenizer)); } return(jString); }
/// <inheritdoc /> public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) { writer.WriteStringValue(JsonStringNode.ToJson(value, true)); }