question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cross Platform Primitive Types with Generics

See original GitHub issue

Source/destination types

public class TestClass<T>
{
    public int A { get; set; }
    public T B { get; set; }
}

Source/destination JSON

With UWP/.NetCore:

{
    "$type": "AppTest.MainPage+TestClass`1[[System.Int32, System.Private.CoreLib]], AppTest",
    "A": 1,
    "B": 2
}

With .NET Framework

{
    "$type": "AppTest.MainPage+TestClass`1[[System.Int32, mscorlib]], AppTest",
    "A": 1,
    "B": 2
}

Expected behavior

Successful deserialization. Or the suggested fix of serializing primitives without the assembly name:

{
    "$type": "AppTest.MainPage+TestClass`1[[System.Int32]], AppTest",
    "A": 1,
    "B": 2
}

Actual behavior

Runtime Exception:

Newtonsoft.Json.JsonSerializationException: ‘Error resolving type specified in JSON ‘ConsoleApp3.Program+TestClass`1[[System.Int32, mscorlib]], ConsoleApp3’. Path ‘$type’, line 1, position 81.’

Steps to reproduce

  1. Serialize class with TypeNameHandling (Tested with TypeNameHandling.Objects)
 string serialized = JsonConvert.SerializeObject(a, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Objects});
  1. Deserialize json file on a different .Net system a. That is, .NETFramework if UWP/.NETCore, and vice versa.
object deserialized = JsonConvert.DeserializeObject(serialized, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Objects });

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:19 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
SimonCroppcommented, Dec 4, 2020

@danebou i didnt consider my suggestion a “fix”, just a workaround. My preference would be that this scenario was supported OOTB

1reaction
SimonCroppcommented, Nov 26, 2020

potential workaround?

    AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
    {
        if (args.Name == "System.Private.CoreLib")
        {
            return typeof(int).Assembly;
        }

        return null;
    };
Read more comments on GitHub >

github_iconTop Results From Across the Web

JEP 218: Generics over Primitive Types
Summary. Extend generic types to support the specialization of generic classes and interfaces over primitive types.
Read more >
Generics for Native Types (Primitives) in Java (or ...
I'm currently using two classes which both represent a data structure (sth like a special buffer, but to keep things simple in this...
Read more >
Can you use primitive types for generic parameters in a ...
No. Generics aren't meant to represent primitives. They're meant to represent objects (as the generic types all extend Object implicitly).
Read more >
Java Generics - No Primitive Types
Using generics, primitive types can not be passed as type parameters. In the example given below, if we pass int primitive type to...
Read more >
OpenJDK proposals would bring universal generics to Java
Universal generics would allow Java type variables to range over primitive and reference types, making it easier to extend or reuse code ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found