Giter Club home page Giter Club logo

Comments (7)

manuc66 avatar manuc66 commented on June 2, 2024

Hi,

Here is the not working solution https://dotnetfiddle.net/wBWRMw

The is no enough information in .RegisterSubtype(typeof(Some<>), "Some") to know which target type need to be instantiated, how would you resole to the fact that it should deserialize to Some<int> ?

from jsonsubtypes.

EdeMeijer avatar EdeMeijer commented on June 2, 2024

Yeah, I figured it would probably be impossible to deserialize. But what about only serialization? In this particular instance, that's really the only thing I need.

That said, I'd understand if you would not want to add additional logic for a serialize-only solution.

from jsonsubtypes.

EdeMeijer avatar EdeMeijer commented on June 2, 2024

Actually, this just made me think. Why not also use extra JSON properties to encode the generic type(s)? The above case could be serialized to {"Type": "Some", "T": "System.Int32", "Value": 42} by default, and you could introduce extra attributes to annotate the type parameters in order to customize the JSON property. Then this information could be used to determine the concrete type during deserialization.

I can imagine that including raw system type names (and any actual type names for that matter) in JSON is not desirable (not portable, potential security risk). In the same fashion as the "known subtypes" approach, maybe the user could configure a custom mapping of supported generic types to type names to use in the serialized JSON. Something like this:

[JsonSubtypes.KnownGenericType(typeof(int), "Integer")]
[JsonSubtypes.KnownGenericType(typeof(string), "Text")]
[JsonSubtypes.KnownGenericType(typeof(IResult), "Result")]
public class Some<[JsonSubtypes.GenericType("ResultType")] T> : IResult
{
    public T Value { get; set; }
}

With the above configuration, the serialized JSON would look like {"Type": "Some", "ResultType": "Integer", "Value": 42}.

I understand this is quite the extension to your library and might be out of scope, but I think it could be a valuable addition for some users.

from jsonsubtypes.

manuc66 avatar manuc66 commented on June 2, 2024

Hi @EdeMeijer

The following sample does not work with the lines in comment, but it seems to be close to something that already exists:

using System;
using JsonSubTypes;
using Newtonsoft.Json;

namespace subTypesIssue
{
    public class Program
    {
        public static void Main()
        {
      
            var input = new SomeInteger {Value = 42};
            var json = JsonConvert.SerializeObject(input);

            Console.WriteLine(json);
			
            var result = JsonConvert.DeserializeObject<IResult>(json);

            Console.WriteLine(result);
        }
    }
			
	[JsonConverter(typeof(JsonSubtypes), "Type")]	
	[JsonSubtypes.KnownSubType(typeof(Some<>), "Some")]												
	public interface IResult
    {
		string Type { get;}
    }

	public class SomeInteger: Some<int> {
		public override string ResultType { get { return "SomeInteger"; }} 
		
	}

	public class SomeText: Some<string> {
		public override string ResultType { get { return "SomeText"; }} 
		
	}

	[JsonConverter(typeof(JsonSubtypes), "ResultType")]
	//[JsonSubtypes.KnownSubType(typeof(SomeInteger), "SomeInteger")]
	//[JsonSubtypes.KnownSubType(typeof(SomeText), "SomeText")]
	public abstract class Some<T> : IResult
	{
	
		public string Type { get { return "Some"; }} 
		public abstract string ResultType { get; }
		public T Value { get; set; }
	}
}

See: https://dotnetfiddle.net/EGqf2C

I've not debug yet the reason why...

from jsonsubtypes.

EdeMeijer avatar EdeMeijer commented on June 2, 2024

That's... interesting. You're stacking two [JsonConverter] attributes in the same type hierarchy. But even is this would work, it would require the user to still declare every type he might be using as a sub type. Not exactly what I had in mind.

That said, for my current use case, I worked around the problem in a different way (using come custom wrapper type) so I'd say, don't put too much effort into this if you don't feel like this is very important to you.

from jsonsubtypes.

manuc66 avatar manuc66 commented on June 2, 2024

Some generic handling implemented in : #140

from jsonsubtypes.

manuc66 avatar manuc66 commented on June 2, 2024

Published with version 1.9.0

from jsonsubtypes.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.