Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/StreamJsonRpc/Protocol/JsonRpcError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

using System.Diagnostics;
using System.Runtime.Serialization;
using System.Text.Json.Nodes;
using StreamJsonRpc.Reflection;
using JsonNET = Newtonsoft.Json.Linq;
using STJ = System.Text.Json.Serialization;

namespace StreamJsonRpc.Protocol;
Expand Down Expand Up @@ -51,15 +51,15 @@ public object? Id
/// <inheritdoc/>
public override string ToString()
{
return new JsonNET.JObject
return new JsonObject
{
new JsonNET.JProperty("id", this.RequestId.ObjectValue),
new JsonNET.JProperty("error", new JsonNET.JObject
["id"] = this.RequestId.AsJsonValue(),
["error"] = new JsonObject
{
new JsonNET.JProperty("code", this.Error?.Code),
new JsonNET.JProperty("message", this.Error?.Message),
}),
}.ToString(Newtonsoft.Json.Formatting.None);
["code"] = this.Error?.Code is not null ? JsonValue.Create((int)this.Error.Code) : null,
["message"] = this.Error?.Message,
},
}.ToJsonString();
}

/// <summary>
Expand Down
10 changes: 5 additions & 5 deletions src/StreamJsonRpc/Protocol/JsonRpcRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Diagnostics;
using System.Reflection;
using System.Runtime.Serialization;
using JsonNET = Newtonsoft.Json.Linq;
using System.Text.Json.Nodes;
using STJ = System.Text.Json.Serialization;

namespace StreamJsonRpc.Protocol;
Expand Down Expand Up @@ -285,10 +285,10 @@ public virtual bool TryGetArgumentByNameOrIndex(string? name, int position, Type
/// <inheritdoc/>
public override string ToString()
{
return new JsonNET.JObject
return new JsonObject
{
new JsonNET.JProperty("id", this.RequestId.ObjectValue),
new JsonNET.JProperty("method", this.Method),
}.ToString(Newtonsoft.Json.Formatting.None);
["id"] = this.RequestId.AsJsonValue(),
["method"] = this.Method,
}.ToJsonString();
}
}
8 changes: 4 additions & 4 deletions src/StreamJsonRpc/Protocol/JsonRpcResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System.Diagnostics;
using System.Runtime.Serialization;
using JsonNET = Newtonsoft.Json.Linq;
using System.Text.Json.Nodes;
using STJ = System.Text.Json.Serialization;

namespace StreamJsonRpc.Protocol;
Expand Down Expand Up @@ -71,10 +71,10 @@ public object? Id
/// <inheritdoc/>
public override string ToString()
{
return new JsonNET.JObject
return new JsonObject
{
new JsonNET.JProperty("id", this.RequestId.ObjectValue),
}.ToString(Newtonsoft.Json.Formatting.None);
["id"] = this.RequestId.AsJsonValue(),
}.ToJsonString();
}

/// <summary>
Expand Down
5 changes: 5 additions & 0 deletions src/StreamJsonRpc/RequestId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Diagnostics;
using System.Globalization;
using System.Text.Json.Nodes;
using Newtonsoft.Json;

namespace StreamJsonRpc;
Expand Down Expand Up @@ -122,4 +123,8 @@ internal static RequestId Parse(object? value)
value is int i ? new RequestId(i) :
throw new JsonSerializationException("Unexpected type for id property: " + value.GetType().Name);
}

internal JsonValue? AsJsonValue() =>
this.Number is not null ? JsonValue.Create(this.Number.Value) :
JsonValue.Create(this.String);
}
44 changes: 41 additions & 3 deletions test/StreamJsonRpc.Tests/Protocol/JsonRpcRequestTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using StreamJsonRpc.Protocol;
using Xunit;

public class JsonRpcRequestTests
{
private static readonly IReadOnlyList<object> ArgumentsAsList = new List<object> { 4, 6, 8 };
Expand Down Expand Up @@ -81,4 +78,45 @@ public void DefaultRequestDoesNotSupportTopLevelProperties()
Assert.False(request.TryGetTopLevelProperty<string>("test", out string? value));
}
#pragma warning restore CS0618 // Type or member is obsolete

[Fact]
public void ToString_Works()
{
var data = new JsonRpcRequest
{
RequestId = new RequestId(10),
Method = "t",
};

Assert.Equal(
"""{"id":10,"method":"t"}""",
data.ToString());
}

[Fact]
public void JsonRpcError_ToString_Works()
{
var data = new JsonRpcError
{
RequestId = new RequestId(1),
Error = new JsonRpcError.ErrorDetail { Code = JsonRpcErrorCode.InternalError, Message = "some error" },
};

Assert.Equal(
"""{"id":1,"error":{"code":-32603,"message":"some error"}}""",
data.ToString());
}

[Fact]
public void JsonRpcResult_ToString_Works()
{
var data = new JsonRpcResult
{
RequestId = new RequestId("id"),
};

Assert.Equal(
"""{"id":"id"}""",
data.ToString());
}
}