Comments (7)
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.
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.
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.
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.
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.
Some generic handling implemented in : #140
from jsonsubtypes.
Published with version 1.9.0
from jsonsubtypes.
Related Issues (20)
- Support Enums HOT 2
- Misleading error message on deserialization when subtype is unknown HOT 1
- Allow to disable behavior introduced by fix #100 in 1.8.0 - Stop StopLookupOnMatch HOT 2
- Incorrect link to license
- New Major to depend on newer Newtonsoft.Json HOT 3
- KnownSubTypeWithProperty it would be great to support aliases HOT 3
- MaxDepth not copied over when creating internal JsonReader
- Serialize type discriminator when using attributes too HOT 6
- Use in API calls?
- Example code in readme fails, just completely wrong according to visual studio HOT 1
- JsonSubtypesConverterBuilder SetFallbackSubtype HOT 1
- wrong link in v2.0.1 release notes HOT 1
- JsonReaderException does not contain a constructor that takes 5 arguments HOT 4
- Persistent settings HOT 1
- Base Class Properties not being Deserialized HOT 2
- Outdated version of NewtsonSoft.Json used
- deserializing dates with jsonsubtypes does not match same result values as stock newtonsoft
- Error on deserialization "Could not find member ... on type" when MissingMemberHandling=error and there is no discriminator field on the dto HOT 1
- Support combination of independent discriminator types per inheritance level HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonsubtypes.