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

[API Proposal]: Add IPAddress, IPEndPoint, DnsEndPoint, PhysicalAddress json built-in converters #97494

Closed
DrkWzrd opened this issue Jan 25, 2024 · 3 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json

Comments

@DrkWzrd
Copy link

DrkWzrd commented Jan 25, 2024

Background and motivation

Sorry for my english.

#83201 should coming with another convenience implementations, like mentioned in issue title.

API Proposal

Something like this:

class IPAddressConverter : JsonConverter<IPAddress>
{
	public override IPAddress? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
	{
		var token = reader.GetString();
		return token is null ? null : IPAddress.Parse(token);
	}

	public override void Write(Utf8JsonWriter writer, IPAddress value, JsonSerializerOptions options)
	{
		writer.WriteStringValue(value.ToString());
	}
}

class PhysicalAddressConverter : JsonConverter<PhysicalAddress>
{

	public override PhysicalAddress? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
	{
		var token = reader.GetString();
		return token is null ? null : PhysicalAddress.Parse(token);
	}

	public override void Write(Utf8JsonWriter writer, PhysicalAddress value, JsonSerializerOptions options)
	{
		writer.WriteStringValue(value.ToString());
	}
}

API Usage

var ip = new IPAddress("192.168.1.1");
var json = JsonSerializer.Serialize(ip);

Alternative Designs

No response

Risks

I can't think any.

@DrkWzrd DrkWzrd added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jan 25, 2024
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jan 25, 2024
@ghost
Copy link

ghost commented Jan 25, 2024

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

Issue Details

Background and motivation

Sorry for my english.

#83201 should coming with another convenience implementations, like mentioned in issue title.

API Proposal

Something like this:

class IPAddressConverter : JsonConverter<IPAddress>
{
	public override IPAddress? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
	{
		var token = reader.GetString();
		return token is null ? null : IPAddress.Parse(token);
	}

	public override void Write(Utf8JsonWriter writer, IPAddress value, JsonSerializerOptions options)
	{
		writer.WriteStringValue(value.ToString());
	}
}

class PhysicalAddressConverter : JsonConverter<PhysicalAddress>
{

	public override PhysicalAddress? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
	{
		var token = reader.GetString();
		return token is null ? null : PhysicalAddress.Parse(token);
	}

	public override void Write(Utf8JsonWriter writer, PhysicalAddress value, JsonSerializerOptions options)
	{
		writer.WriteStringValue(value.ToString());
	}
}

API Usage

var ip = new IPAddress("192.168.1.1");
var json = JsonSerializer.Serialize(ip);

Alternative Designs

No response

Risks

I can't think any.

Author: DrkWzrd
Assignees: -
Labels:

api-suggestion, area-System.Text.Json

Milestone: -

@eiriktsarpalis
Copy link
Member

It's not something we could support out of the box, since STJ can't take a dependency on networking components. As usual the recommendation would be to author custom converters for the types that you need to serialize.

@eiriktsarpalis eiriktsarpalis closed this as not planned Won't fix, can't repro, duplicate, stale Jan 25, 2024
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Jan 25, 2024
@gregsdennis
Copy link
Contributor

Once you've coded the converters, it'd make a great Nuget package. In fact, I'd be surprised if there's not already one out there.

@github-actions github-actions bot locked and limited conversation to collaborators Feb 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Text.Json
Projects
None yet
Development

No branches or pull requests

3 participants