public void Format_ReturnsFormattedLiteral() { // Arrange IJsonObject j0 = JsonObject.FromString("{foo: 'fooval', bar :[], baz: { baz2: [0,1,2, {}] }}"); IJsonObject j1 = JsonObject.Builder().Build(); IJsonObject j2 = JsonObject.OfNull(); IJsonFormatOptions opts = JsonFormatOptions.Builder().Indent(true).NewLine("\n").Build(); // Act string s0 = j0.Format(opts); string s1 = j1.Format(opts); string s2 = j2.Format(opts); // Assert Assert.AreEqual("{\n" + "\t\"foo\" : \"fooval\",\n" + "\t\"bar\" : [],\n" + "\t\"baz\" : {\n" + "\t\t\"baz2\" : [\n" + "\t\t\t0,\n" + "\t\t\t1,\n" + "\t\t\t2,\n" + "\t\t\t{}\n" + "\t\t]\n" + "\t}\n" + "}", s0); Assert.AreEqual("{}", s1); Assert.AreEqual("null", s2); }
/// <summary> /// APIリクエスト"getTaskExecutionStatus"を実行します。 /// </summary> /// <returns>APIから返却されたJSON</returns> /// <param name="ps">パラメータ</param> /// <param name="ctx">実行コンテキスト</param> public IJsonObject RequestGetTaskExecutionStatus(Parameters ps, Context ctx) { var req = MakeRequest(ps, JsonObject.Builder() .Append("actionName", "getTaskExecutionStatus") .Append("taskId", ctx.TaskId) .Append("execRequestId", ctx.ExecRequestId) .Build()); return(SendRequest(ps, ctx, req)); }
/// <summary> /// JSONデータをシリアライズします。 /// ただし認証情報が含まれている場合はそれをマスキングします。 /// </summary> /// <returns>シリアライズされたJSONデータ</returns> /// <param name="j">デシリアライズされたJSONデータ</param> public string SerializeSecurely(IJsonObject j) { var b = JsonObject.Builder(j); if (j.HasProperty("authPass")) { b.Append("authPass", "*****"); } return(b.Build().Format(formatOptions)); }
/// <summary> /// APIリクエスト"taskLog"を実行します。 /// </summary> /// <returns>APIから返却されたJSON</returns> /// <param name="ps">パラメータ</param> /// <param name="ctx">実行コンテキスト</param> public IJsonObject RequestTaskLog(Parameters ps, Context ctx) { var req = MakeRequest(ps, JsonObject.Builder() .Append("actionName", "taskLog") .Append("taskId", ctx.TaskId) .Append("lastExecution", true) .Build()); return(SendRequest(ps, ctx, req)); }
/// <summary> /// RPCリクエストを標準出力にダンプします. /// </summary> /// <param name="req">レスポンス.</param> public void DumpRequest(IRequest req) { var b = JsonObject.Builder(req.Parameters); if (req.Parameters.HasProperty("authPass")) { b.Append("authPass", "*****"); } PrintMessage("リクエストURI", req.Uri); PrintMessage("リクエストJSON", b.Build().Format(formatOptions)); }
public void Build_WhenNoPropertyAppended_ReturnsEmptyObject() { // Arrange var b0 = JsonObject.Builder(); // Act var r0 = b0.Build(); // Assert Assert.That(r0.TypeIs(JsonObjectType.Object), Is.True); Assert.That(r0.ToString(), Is.EqualTo("{}")); Assert.That(r0.Properties.Count(), Is.EqualTo(0)); }
public void Append_OverwritesOldPropertyWithNewPropertyHasSameName() { // Arrange var b0 = JsonObject.Builder(); // Act var r0 = b0.Append("foo", "bar").Append("foo", "baz").Build(); // Assert Assert.That(r0.TypeIs(JsonObjectType.Object), Is.True); Assert.That(r0.ToString(), Is.EqualTo("{\"foo\":\"baz\"}")); Assert.That(r0.Properties.Count(), Is.EqualTo(1)); }
/// <summary> /// APIリクエスト"getTaskStatus"を実行します。 /// </summary> /// <returns>APIから返却されたJSON</returns> /// <param name="ps">パラメータ</param> /// <param name="ctx">実行コンテキスト</param> public IJsonObject RequestGetTaskStatus(Parameters ps, Context ctx) { var req = MakeRequest(ps, JsonObject.Builder() .Append("actionName", "getTaskStatus") .Append("taskId", ctx.TaskId) .Build()); var resp = SendRequest(ps, ctx, req); if (resp.HasProperty("status")) { return(resp); } throw MakeException("Unexpected result of API calling. " + "\"getTaskStatus\" does not return \"status\"."); }
/// <summary> /// APIリクエスト"runTask"を実行します。 /// </summary> /// <returns>APIから返却されたJSON</returns> /// <param name="ps">パラメータ</param> /// <param name="ctx">実行コンテキスト</param> public IJsonObject RequestRunTask(Parameters ps, Context ctx) { var req = MakeRequest(ps, JsonObject.Builder() .Append("actionName", "runTask") .Append("taskId", ctx.TaskId) .Append("mode", "asynchronous") .Build()); var resp = SendRequest(ps, ctx, req); if (resp.HasProperty("execRequestId")) { return(resp); } throw MakeException("Unexpected result of API calling. " + "\"runTask\" does not return \"execRequestId\"."); }
IJsonObject ParseObjectNode(Input input) { input.Check('{'); input.GoNext(); var b = JsonObject.Builder(); input.SkipWhitespace(); if (input.Current == '}') { input.GoNext(); return(b.Build()); } while (!input.EndOfFile) { string propName; if (input.Current == '"' || input.Current == '\'') { propName = ParseQuotedString(input); } else { propName = ParseIdentifierString(input); } input.SkipWhitespace(); input.Check(':'); input.GoNext(); input.SkipWhitespace(); b.Append(propName, ParseNode(input)); input.SkipWhitespace(); if (input.Current == '}') { input.GoNext(); return(b.Build()); } input.Check(','); input.GoNext(); input.SkipWhitespace(); } throw new ParseException(input, "unclosed Object literal."); }
/// <summary> /// DryRunモードでHTTPクライアントのモックとして使用されるメソッドです。 /// </summary> /// <returns>APIレスポンス</returns> /// <param name="req">APIリクエスト</param> public IResponse DelegateAgent(IRequest req) { var b = Response .Builder() .Request(req) .StatusCode(System.Net.HttpStatusCode.OK); var b2 = JsonObject .Builder() .Append("returnCode", 0) .Append("taskId", 123); if (req.ActionName.Equals("getTaskIdByName")) { return(b.Body(b2.Build()) .Build()); } else if (req.ActionName.Equals("getTaskStatus")) { return(b.Body(b2.Append("status", "READY_TO_RUN") .Build()) .Build()); } else if (req.ActionName.Equals("runTask")) { return(b.Body(b2.Append("execRequestId", "123_abc") .Build()) .Build()); } else if (req.ActionName.Equals("getTaskExecutionStatus")) { return(b.Body(b2.Append("jobExitCode", 0) .Build()) .Build()); } else if (req.ActionName.Equals("taskLog")) { return(b.Body(b2.Build()) .Build()); } else { return(null); } }
public IResponse MakeResponse(IRequest req, HttpStatusCode status, int returnCode, Action <JsonObjectBuilder> modifier) { var b = Response .Builder() .Request(req) .StatusCode(status); var b2 = JsonObject .Builder() .Append("returnCode", returnCode); if (modifier != null) { modifier(b2); } return(b.Body(b2.Build()).Build()); }
public void SendAsync_ExecutesHttpRequestViaAgentAsynchronously_ThenReturnsIResponseInstance() { // Arrange var req0 = Request .Builder() .Agent(MakeMockAgent(JsonObject .Builder() .Append("foo", "bar") .Build() )) .Build(); // Act // Assert Assert.That(async() => { var r = await req0.SendAsync(); return(r.Body.GetProperty("foo").AsString()); }, Is.EqualTo("bar")); }
public static void Main(string[] args) { // 1. Read JSON // JsonObject.FromXxxx methods read JSON from String, File and Stream. IJsonObject j1 = JsonObject.FromString("{foo: 123, bar: true, baz: \"hello\"}"); // IJsonObject#GetProperty() accesses JSON object's property. // NOTE: GetPropery() and XxxxValue() methods have another version // that can be specified fallback value. Console.WriteLine("j1 = {0}", j1); Console.WriteLine("foo = {0}", j1.GetProperty("foo").AsNumber()); Console.WriteLine("bar = {0}", j1.GetProperty("bar").AsBoolean()); Console.WriteLine("baz = {0}", j1.GetProperty("baz").AsString()); Console.WriteLine("baa exists? = {0}", j1.HasProperty("baa")); // 2. Build JSON // JsonObject.Builder() returns new builder instance. IJsonObject j2 = JsonObject .Builder() .Append("foo", "hello") .Append("bar", "hello", "bonjour", "こんにちは") .Append("baz", (b) => { // Lambda's argument is new builder // instance for nested object. b.Append("bazProp1", 1); b.Append("bazProp2", 2); }) .Build(); // The builder can be initialized with existed JSON. IJsonObject j3 = JsonObject .Builder(j2).Append("baa", 123).Build(); Console.WriteLine("j2 = {0}", j2); Console.WriteLine("j3 = {0}", j3); // 3. Format JSON IJsonFormatOptions opts = JsonFormatOptions .Builder() .Indent(true).SoftTabs(true).TabWidth(2) .Build(); Console.WriteLine("j3' = {0}", j3.Format(opts)); }
public void Send_ExecutesHttpRequestViaAgent_ThenReturnsIResponseInstance() { // Arrange var req0 = Request .Builder() .Agent(MakeMockAgent(JsonObject .Builder() .Append("foo", "bar") .Build() )) .Build(); // Act IResponse resp0 = req0.Send(); // Assert Assert.That(resp0.StatusCode, Is.EqualTo(HttpStatusCode.OK)); Assert.That(resp0.Body.GetProperty("foo").AsString(), Is.EqualTo("bar")); Assert.That(resp0.Request, Is.EqualTo(req0)); }
public void Build_WhenOnePropertyAppended_ReturnsObjectHasOneProperty() { // Arrange var b0 = JsonObject.Builder(); var b1 = JsonObject.Builder(); var b2 = JsonObject.Builder(); var b3 = JsonObject.Builder(); // Act var r0 = b0.Append("foo", "bar").Build(); var r1 = b1.AppendEmptyArray("foo").Build(); var r2 = b2.AppendNull("foo").Build(); var r3 = b3.Append("foo", new double[] { 1, 2, 3 }).Build(); // Assert Assert.That(r0.TypeIs(JsonObjectType.Object), Is.True); Assert.That(r0.ToString(), Is.EqualTo("{\"foo\":\"bar\"}")); Assert.That(r0.Properties.Count(), Is.EqualTo(1)); Assert.That(r1.ToString(), Is.EqualTo("{\"foo\":[]}")); Assert.That(r2.ToString(), Is.EqualTo("{\"foo\":null}")); Assert.That(r3.ToString(), Is.EqualTo("{\"foo\":[1,2,3]}")); }
IJsonObject PairsToObject(Seq <Tuple <IJsonObject, IJsonObject> > pairs) { return(pairs.Aggregate(JsonObject.Builder(), (a0, a1) => a0.Append(a1.Item1.AsString(), a1.Item2), a0 => a0.Build())); }