Discussion:
JSON to Scala class with GSON - SerializedName annotation does not work
Tim
12 years ago
Permalink
I have a simple Scala class:

import com.google.gson.annotations.SerializedName
extends Serializable { ... }
And I have this code:

val testJson = "{\"myTest\": \"my-test\"}"
val myTest = gson.fromJson(testJson, classOf[MyTest])
This works fine! The content of the variable of *myTest* is *my-test*.

In reality, my JSON looks like this:

val testJsonWithUnderscore = "{\"my_test\": \"my-test\"}"
val myTest = gson.fromJson(testJsonWithUnderscore, classOf[MyTest])
So, the variable name *myTest* does not match with the *my_test* of the
JSON string. So I included *@SerializedName("my_test")* but it does not
work. The var *myTest* is *null*.

The *@SerializedName("my_test")* does not work like expected. What is wrong
here?
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
Shail Mehta
12 years ago
Permalink
Hi Tim -
I have exactly the same issue. Were you able to find a resolution?

thanks
Post by Tim
import com.google.gson.annotations.SerializedName
extends Serializable { ... }
val testJson = "{\"myTest\": \"my-test\"}"
val myTest = gson.fromJson(testJson, classOf[MyTest])
This works fine! The content of the variable of *myTest* is *my-test*.
val testJsonWithUnderscore = "{\"my_test\": \"my-test\"}"
val myTest = gson.fromJson(testJsonWithUnderscore, classOf[MyTest])
So, the variable name *myTest* does not match with the *my_test* of the
work. The var *myTest* is *null*.
wrong here?
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
ehud via scala-user
9 years ago
Permalink
You can just add it as var in body for example:

class MyTest(){
@SerializedName("my_test")
var myTest: String = null
}
...
--
Gigya <http://www.gigya.com/> | The Leader in Customer Identity Management
2016 CRM Watchlist Winner
<http://www.zdnet.com/article/and-the-winners-of-the-2016-crm-watchlist-are/>
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Raul Bache
9 years ago
Permalink
https://issues.scala-lang.org/plugins/servlet/mobile#issue/SI-8975
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...