vavr-io / vavr-gson Goto Github PK
View Code? Open in Web Editor NEWGson datatype module for Vavr
License: Apache License 2.0
Gson datatype module for Vavr
License: Apache License 2.0
Hi @before,
great to have you onboard :)
In a first step we should setup the javaslang-gson repository first an initial commit.
You may take the javaslang-jackson project as template. It is a Maven layout. Or you may choose Gradle for the build - your call.
Please ask on any questions - I will assist you whenever possible.
Greets
- Daniel
When a class contains a field with a vavr interface type, e.g. Map
, Gson can't deserialize it, as it needs a certain implementation of Map
. Often a particular implementation (like HashMap
) would be a good default for the majority of classes, so one could just register a TypeHierarchyAdapter
using a HashMap
to deserialize a Map
field. Such TypeHierarchyAdapter
s could be part of the library (but not registered in VavrGson
, just as additional helpers).
hi!
Serializing an object with Seq
members serializes that object as empty list, even when the sequence has items in it:
import io.vavr.API.List
import io.vavr.collection.List
import io.vavr.collection.Seq
import io.vavr.gson.VavrGson
import org.junit.Test
import kotlin.test.assertEquals
class HelloTest {
private val expected: String = "{\"ints\":[1,2,3]}"
data class DataWithSeq(val ints: Seq<Int>)
data class DataWithList(val ints: List<Int>)
@Test
fun testWithSeq() { // <- fails
val data = DataWithSeq(List(1, 2, 3))
assertEquals(expected, toJson(data))
}
@Test
fun testWithList() {
val data = DataWithList(List(1, 2, 3))
assertEquals(expected, toJson(data))
}
private fun toJson(any: Any): String {
val builder = GsonBuilder()
VavrGson.registerAll(builder)
return builder.create().toJson(any)
}
}
testWithSeq fails with
org.junit.ComparisonFailure:
Expected :{"ints":[1,2,3]}
Actual :{"ints":{}}
i only tested it in kotlin not in java but since Seq
seems not to be among the registered factories in Gson.factories it is probably not different there.
tested with
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr</artifactId>
<version>0.9.2</version>
</dependency>
<dependency>
<groupId>io.vavr</groupId>
<artifactId>vavr-gson</artifactId>
<version>0.9.2</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.