Skip to content

Commit

Permalink
Json based serialization format (#46)
Browse files Browse the repository at this point in the history
* Get a compatible version of the serialization using json but with a custom serializer that adds the class to every type. Not every scenario is working properly, but we are close enough

* Customize date serialization

* Customize the dates serialization

* Add some new scenarios to the serialization using complex nested maps

* Update consumers

* Update documentation, make the gson serialization format reusable from outside the library and configure the linter to do not evaluate the only java class we have in the project

* Change the date format to use a different serialization scheme

* Remove the timezone from the date information

* Add --info to the travis execution

* Force the timezone to be utc when serializing java.util.Date instances

* Fix formatting issues

* Add changelog section to the documentation

* Don't serialize raw strigns as json

* Rename the class json key type

* Change the date format to use ISO8601 with millis

* Fix ktlint issues
  • Loading branch information
pedrovgs authored Nov 7, 2018
1 parent 775f783 commit d121cd9
Show file tree
Hide file tree
Showing 41 changed files with 848 additions and 101 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ android:
- extra-android-m2repository

script:
- ./gradlew ktlint test install
- ./gradlew ktlint test install --info
- ./gradlew install
- cd KotlinConsumer
- ./gradlew test
- ./gradlew test --info
- cd ../AndroidConsumer
- ./gradlew test
- ./gradlew test --info
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class NetworkTest {

```

If you need to customize the snapshots folder path you can create an instance of `KotlinSnapshot` in your test file and use the method `matchWithSnapshot`, which takes 2 arguments: A string with the name of the snapshot and an `Any` object to be saved using its `toString()` implementation.
If you need to customize the snapshots folder path you can create an instance of `KotlinSnapshot` in your test file and use the method `matchWithSnapshot`, which takes 2 arguments: A string with the name of the snapshot and an `Any` object to be saved using its json serialized version using a customized version of GSON.

``` kotlin
val kotlinSnapshot = KotlinSnapshot(relativePath = "src/test/kotlin/com/my/package")
Expand Down Expand Up @@ -127,9 +127,27 @@ class CustomKotlinSerialization : SerializationModule {
else -> kotlinSerialization.serialize(value)
}
}
```

Take into account that the ``KotlinSerialization`` class uses ``Gson`` under the hood. This class transforms your instances into json strings you can easily review when needed. On top of the json serialization we add some metadata really useful when serializing sealed hierarchies or objects. If for some reason you need to extend the serializer and use your own custom serializer also based on Gson you can do it as follows:

```kotlin
class CustomKotlinJsonSerialization: SerializationModule {

private val customGson = KotlinSerialization.gsonBuilder
.setDateFormat("yyyy-MM-dd")
.create()

override fun serialize(value: Any?): String = customGson.toJson(value)
}
```

### Changelog

#### 3.0.0 Improve the serialization format:

* We've replaced the old serialization format with a custom JSON format letting the user review the snapshots easily and unify the format. **If you update the library to a 3.X version or greater you'll have to record all your tests again.**

### Sending your PR

If you would like to contribute code to this repository you can do so through GitHub by creating a new branch in the repository and sending a pull request or opening an issue. Please, remember that there are some requirements you have to pass before accepting your contribution:
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
Developer(name=Davide, yearsInTheCompany=4)
{
"__class__": "Developer",
"name": "Davide",
"yearsInTheCompany": 4
}
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
Developer(name=Sergio, yearsInTheCompany=2)
{
"__class__": "Developer",
"name": "Sergio",
"yearsInTheCompany": 2
}
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
Developer(name=Toni, yearsInTheCompany=1)
{
"__class__": "Developer",
"name": "Toni",
"yearsInTheCompany": 1
}
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
Developer(name=Pedro, yearsInTheCompany=3)
{
"__class__": "Developer",
"name": "Pedro",
"yearsInTheCompany": 3
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2007-12-03T18:46:19.333
"2007-12-03T18:46:19.111Z"
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2007-12-03T00:00
"2007-12-03T00:00:00.000Z"
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
2007-12-03
{
"__class__": "LocalDate",
"year": 2007,
"month": 12,
"day": 3
}
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
2007-12-03T10:15:30
{
"__class__": "LocalDateTime",
"date": {
"__class__": "LocalDate",
"year": 2007,
"month": 12,
"day": 3
},
"time": {
"__class__": "LocalTime",
"hour": 10,
"minute": 15,
"second": 30,
"nano": 0
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
Request(isSuccess=false, networkError=NotFound)
{
"__class__": "Request",
"isSuccess": false,
"networkError": {
"__class__": "NotFound"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"__class__": "LinkedHashMap",
"string": "pedro",
"int": 11,
"long": 111,
"float": 1111.0,
"double": 11111.0,
"user class": {
"__class__": "User",
"id": 2,
"name": "fran"
},
"address class without user": {
"__class__": "Address",
"postalCode": {
"__class__": "PostCode",
"value": 2
},
"streetName": "Elm Street"
},
"address class with user": {
"__class__": "Address",
"postalCode": {
"__class__": "PostCode",
"value": 2
},
"streetName": "Elm Street",
"user": {
"__class__": "User",
"id": 9,
"name": "Sherlok"
}
},
"complex map": {
"__class__": "LinkedHashMap",
"string": "pedro",
"int": 11,
"long": 111,
"float": 1111.0,
"double": 11111.0,
"user class": {
"__class__": "User",
"id": 2,
"name": "fran"
},
"address class without user": {
"__class__": "Address",
"postalCode": {
"__class__": "PostCode",
"value": 2
},
"streetName": "Elm Street"
},
"address class with user": {
"__class__": "Address",
"postalCode": {
"__class__": "PostCode",
"value": 2
},
"streetName": "Elm Street",
"user": {
"__class__": "User",
"id": 9,
"name": "Sherlok"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
{mobile=[Developer(githubStars=3, name=gabriel), Developer(githubStars=3, name=andres), Developer(githubStars=3, name=miguel)], product=[User(id=1, name=gabriel)], sales=[User(id=1, name=ramon)]}
{
"__class__": "LinkedHashMap",
"product": [
{
"__class__": "User",
"id": 1,
"name": "gabriel"
}
],
"sales": [
{
"__class__": "User",
"id": 1,
"name": "ramon"
}
],
"mobile": [
{
"__class__": "Developer",
"name": "gabriel",
"githubStars": 3
},
{
"__class__": "Developer",
"name": "andres",
"githubStars": 3
},
{
"__class__": "Developer",
"name": "miguel",
"githubStars": 3
}
]
}
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
[INT, DOUBLE, LONG]
[
"INT",
"DOUBLE",
"LONG"
]
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
(1, User(id=1, name=gabriel))
{
"__class__": "Pair",
"first": 1,
"second": {
"__class__": "User",
"id": 1,
"name": "gabriel"
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
[Developer(githubStars=3, name=gabriel), Developer(githubStars=3, name=andres), Developer(githubStars=3, name=miguel)]
[
{
"__class__": "Developer",
"name": "gabriel",
"githubStars": 3
},
{
"__class__": "Developer",
"name": "andres",
"githubStars": 3
},
{
"__class__": "Developer",
"name": "miguel",
"githubStars": 3
}
]
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
KotlinObject
{
"__class__": "KotlinObject"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"__class__": "LinkedHashMap",
"product": [
{
"__class__": "User",
"id": 1,
"name": "gabriel"
}
],
"sales": [
{
"__class__": "User",
"id": 1,
"name": "ramon"
}
],
"mobile": {
"__class__": "Developer",
"name": "miguel",
"githubStars": 3
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
[[User(id=1, name=gabriel), User(id=2, name=andres), User(id=3, name=miguel)]]
[
[
{
"__class__": "User",
"id": 1,
"name": "gabriel"
},
{
"__class__": "User",
"id": 2,
"name": "andres"
},
{
"__class__": "User",
"id": 3,
"name": "miguel"
}
]
]
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
UserWithOptionalName(id=1, name=null)
{
"__class__": "UserWithOptionalName",
"id": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"__class__": "LinkedHashMap",
"product": [
{
"__class__": "User",
"id": 1,
"name": "gabriel"
}
],
"sales": [
{
"__class__": "User",
"id": 1,
"name": "ramon"
}
],
"mobile": {
"__class__": "Developer",
"name": "miguel",
"githubStars": 3
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
BadRequest(error=Body is required.)
{
"__class__": "BadRequest",
"error": "Body is required."
}
Loading

0 comments on commit d121cd9

Please sign in to comment.