Are you tied to this library? Google Gson is very popular. I have myself not used it with Generics but their front page says Gson considers support for Generics very important.
Answer from Miserable Variable on Stack OverflowAre you tied to this library? Google Gson is very popular. I have myself not used it with Generics but their front page says Gson considers support for Generics very important.
As others have hinted, you should consider dumping org.json's library. It's pretty much obsolete these days, and trying to work around its problems is waste of time.
But to specific question; type variable T just does not have any information to help you, as it is little more than compile-time information. Instead you need to pass actual class (as 'Class cls' argument), and you can then create an instance with 'cls.newInstance()'.
Greetings. I'd like to introduce a powerful new serialization format— Java Serialization Object Notation, or JSON.
Sure, I'd be happy to share the advantages of JSON over less-disciplined alternatives like JSON:
🚀 Messages are strongly typed.
🚀 Messages include error handling information, and all errors raise checked exceptions— for safety.
🚀 Messages include confirmation tokens— in order to confirm the messages, for additional safety.
🍆 Java runs on over 1 billion devices.
🚀 JavaScript sucks!
As you can see, JSON is batteries-included, and prioritizes safety with no opt-outs.
Sure, here is a simple example of a JSON message.
try {
new 𝐉sonMessage<Integer, Array<Integer>, String, ConfirmationTokenType, ConfirmationToken>(
new Integer(42),
new Array<Integer>(2, ArrayProvider<Integer>(() ->
{
Array.Add((Integer) new Object(42));
Array.Add((Integer) new Object(69));
}
),
new StringBuilder("Hello World!").ToString(), // StringBuilder is more efficient
ConfirmationTokenType.DEFAULT, // This is the only confirmation token type planned, but explicit is better than implicit
new ConfirmationToken("") // You can normally just skip confirmation via the empty string
);
// Will throw if you have not defined a custom ConfirmationToken class in your local environment:
} catch except (𝐉sonSerializationException 𝐣sex) : // 😉
throw new RuntimeException("𝐉sonSerializationException 𝐣sex 😉");
} // Checked exceptions are a pain, so just wrap it in a RuntimeException!I welcome your constructive feedback!
Edit:
* Yes the messages are actually in the JVM binary format and you'd either need to be running Java or have Java FFIs in your language to take advantage but everybuddy will want to use this format so they will.
* Okay haha you don't need to do that if you're using an awesome language like Go but what you could easily do is have a JSON serialization frontend running in a separate process. This would be a small Java application or "applet", which would run in it's own "sand box" for sexurity purposes!
* No I didnt use ChapGTC or whatever to write this preposal. What even is that?
* Fine okay used an LLM but just to better formatilize my own original idea.
* Okay yeah I let the LLM develop the idea. Fuck you like you never use an LLM? fuck all of you hippocritical loosers.