Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add STJ Attributes #7

Merged
merged 3 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
More json fixes
  • Loading branch information
333fred committed May 15, 2024
commit 6865ed1625889ceb28d5ed6cd10e8c6efea5c663
Original file line number Diff line number Diff line change
@@ -1,60 +1,21 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace Roslyn.LanguageServer.Protocol
{
using System;
using System.Globalization;
using Microsoft.CodeAnalysis.LanguageServer;
using Microsoft.CommonLanguageServerProtocol.Framework;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

/// <summary>
/// TODO: document.
/// </summary>
internal class DocumentUriConverter : JsonConverter
{
/// <inheritdoc/>
public override bool CanConvert(Type objectType)
{
return true;
}

/// <inheritdoc/>
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
reader = reader ?? throw new ArgumentNullException(nameof(reader));
if (reader.TokenType == JsonToken.String)
{
var token = JToken.ReadFrom(reader);
var uri = new Uri(token.ToObject<string>()!);
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

return uri;
}
else if (reader.TokenType == JsonToken.Null)
{
return null;
}
namespace Roslyn.LanguageServer.Protocol;

throw new JsonSerializationException(string.Format(CultureInfo.InvariantCulture, "Unable to deserialize Uri. Unexpected value encountered: {0}", reader.Value));
}

/// <inheritdoc/>
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
writer = writer ?? throw new ArgumentNullException(nameof(writer));
/// <summary>
/// TODO: document.
/// </summary>
internal class DocumentUriConverter : JsonConverter<Uri>
{
public override Uri Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
=> new(reader.GetString()!);

if (value is Uri uri)
{
var token = JToken.FromObject(uri.AbsoluteUri);
token.WriteTo(writer);
}
else
{
throw new ArgumentException($"{nameof(value)} must be of type {nameof(Uri)}");
}
}
}
public override void Write(Utf8JsonWriter writer, Uri value, JsonSerializerOptions options)
=> writer.WriteStringValue(value.AbsoluteUri);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace Roslyn.LanguageServer.Protocol
{
using System;
using System.Globalization;
using Microsoft.CodeAnalysis.LanguageServer;
using Microsoft.CommonLanguageServerProtocol.Framework;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

/// <summary>
/// TODO: document.
/// </summary>
internal class NewtonsoftDocumentUriConverter : JsonConverter
{
/// <inheritdoc/>
public override bool CanConvert(Type objectType)
{
return true;
}

/// <inheritdoc/>
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
reader = reader ?? throw new ArgumentNullException(nameof(reader));
if (reader.TokenType == JsonToken.String)
{
var token = JToken.ReadFrom(reader);
var uri = new Uri(token.ToObject<string>()!);

return uri;
}
else if (reader.TokenType == JsonToken.Null)
{
return null;
}

throw new JsonSerializationException(string.Format(CultureInfo.InvariantCulture, "Unable to deserialize Uri. Unexpected value encountered: {0}", reader.Value));
}

/// <inheritdoc/>
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
writer = writer ?? throw new ArgumentNullException(nameof(writer));

if (value is Uri uri)
{
var token = JToken.FromObject(uri.AbsoluteUri);
token.WriteTo(writer);
}
else
{
throw new ArgumentException($"{nameof(value)} must be of type {nameof(Uri)}");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal class Range : IEquatable<Range>
/// </summary>
[DataMember(Name = "start"), JsonPropertyName("start")]
[JsonProperty(Required = Required.Always)]
public Position Start
public required Position Start
{
get;
set;
Expand All @@ -33,7 +33,7 @@ public Position Start
/// </summary>
[DataMember(Name = "end"), JsonPropertyName("end")]
[JsonProperty(Required = Required.Always)]
public Position End
public required Position End
{
get;
set;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ namespace Roslyn.LanguageServer.Protocol
{
using System;
using System.Runtime.Serialization;
using Newtonsoft.Json;

using NewtonsoftJsonConverter = Newtonsoft.Json.JsonConverterAttribute;

/// <summary>
/// Class which identifies a text document.
Expand All @@ -20,7 +21,7 @@ internal class TextDocumentIdentifier : IEquatable<TextDocumentIdentifier>
/// Gets or sets the URI of the text document.
/// </summary>
[DataMember(Name = "uri"), JsonPropertyName("uri")]
[JsonConverter(typeof(DocumentUriConverter))]
[NewtonsoftJsonConverter(typeof(NewtonsoftDocumentUriConverter)), JsonConverter(typeof(DocumentUriConverter))]
public Uri Uri
{
get;
Expand Down
Loading