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.

GsonBuilder.disableUnsafe

See original GitHub issue
While it is nice to know that Unsafe is only used when no-arg ctors or instance 
creators are not present, it would be better to have a means to enforce it 
cannot be used.  Particularly in google app engine, I'd like to know that the 
code I use offline is safe inside GAE, rather than a delayed 
UnsupportedOperationException

Can you add GsonBuilder.disableUnsafe or similar?

Original issue reported on code.google.com by adrian.f...@gmail.com on 16 May 2012 at 5:58

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:7
  • Comments:15

github_iconTop GitHub Comments

2reactions
fab1ancommented, Aug 11, 2016

That worked, thanks a lot. For the record: my kotlin-code:

/* throw when no default-constructor was found */
object WarnConstructorFactory : TypeAdapterFactory {

    private val wrappers = listOf(
        java.lang.Boolean::class.java,
        java.lang.Character::class.java,
        java.lang.String::class.java,
        java.lang.Byte::class.java,
        java.lang.Short::class.java,
        java.lang.Integer::class.java,
        java.lang.Long::class.java,
        java.lang.Float::class.java,
        java.lang.Double::class.java
    )

    override fun <T> create(gson: Gson, type: TypeToken<T>): TypeAdapter<T>? {

        val rawType = type.rawType
        if (!(rawType in wrappers || rawType.isInterface || 
              rawType.isPrimitive || rawType.isEnum)) {

            check(
                rawType.constructors.any { it.parameterTypes.count() == 0 },
                { "$type has no public no-args constructor" }
            )
        }
        return null;
    }
}
2reactions
fab1ancommented, Jun 12, 2016

Yes. I use kotlin’s data-classes. If I supply default-values for all constructor parameters, a no-args constructor is generated and everything works fine. If I forget a default-value somewhere though, gson initializes the class with all fields set to null, which then crash with a NPE when I access them, even though kotlin “guarantees” them to be not-null.

So I need gson to crash to see that I forgot to set the parameter.

Read more comments on GitHub >

github_iconTop Results From Across the Web

GsonBuilder - gson 2.10 javadoc
Configures Gson to exclude all fields from consideration for serialization and deserialization that do not have the Expose annotation. GsonBuilder.
Read more >
GsonBuilder.disableUnsafe - - Bountysource
Particularly in google app engine, I'd like to know that the code I use offline is safe inside GAE, rather than a delayed...
Read more >
I've trusted you! You promised no null pointer exceptions!
At the time of writing this document, Google has a proposal to add a method disableUnsafe to the GsonBuilder which will enforce the...
Read more >
Android — 一個Google play console上提示的Unsafe ... - JLin
GsonBuilder.disableUnsafe · Issue #445 · google/gson. While it is nice to know that Unsafe is only used when no-arg ctors or instance creators...
Read more >
Hackday 5.0 - our review - Babbel Bytes
At the time of writing this document, Google has a proposal to add a method disableUnsafe to the GsonBuilder which will enforce the...
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