static JsonObjectBuilder PutJobs(JsonObjectBuilder mainBuilder) { return(mainBuilder.PutArray("jobs", jobs => { jobs.Push("chef"); jobs.Push("programmer"); jobs.Push("designer"); })); }
private static JValue From(IEnumerable <KeyValuePair <string, JValue> > obj) { var builder = new JsonObjectBuilder(512); foreach (var member in obj) { builder.Put(member.Key, member.Value); } return(builder.Build()); }
public JsonArrayBuilder PushObject <T>(T value, Action <JsonObjectBuilder, T> push) { EnsureState(); writer.WriteCommaIf(startOffset); var objectBuilder = new JsonObjectBuilder(writer); push(objectBuilder, value); objectBuilder.Dispose(); return(this); }
public JsonObjectBuilder PutObject <T>(string key, T value, Action <JsonObjectBuilder, T> put) { EnsureState(); writer.WriteCommaIf(startOffset); writer.WriteKey(key); var objectBuilder = new JsonObjectBuilder(writer); put(objectBuilder, value); objectBuilder.Dispose(); return(this); }
public void TestObjectBuilderWithoutChaining() { var builder = new JsonObjectBuilder(1024); PutJobs(builder); builder.Put("name", "Mike"); builder.PutNull("children"); var complexObject = builder.Build(); Assert.AreEqual( @"{""jobs"":[""chef"",""programmer"",""designer""],""name"":""Mike"",""children"":null}", complexObject.ToString());
public void TestObjectBuilder() { var simpleObject = new JsonObjectBuilder(16) .Put("id", 10) .Put("name", "John") .Put("age", 29) .Put("alive", true) .PutNull("friends") .Build(); Assert.AreEqual(@"{""id"":10,""name"":""John"",""age"":29,""alive"":true,""friends"":null}", simpleObject.ToString()); Assert.AreEqual(@"{""id"": 10, ""name"": ""John"", ""age"": 29, ""alive"": true, ""friends"": null}", simpleObject.Serialize(2)); var complexObject = new JsonObjectBuilder(64) .Put("name", "Mike") .PutArray("jobs", jobs => jobs.Push("chef").Push("programmer").Push("designer")) .PutNull("children") .Build(); Assert.AreEqual( @"{""name"":""Mike"",""jobs"":[""chef"",""programmer"",""designer""],""children"":null}", complexObject.ToString()); }