Skip to content

Commit

Permalink
Merge pull request #152 from mincong-h/readme
Browse files Browse the repository at this point in the history
Update serialization/deserialization in README
  • Loading branch information
mincong-h authored Mar 29, 2020
2 parents 986a80f + 333b296 commit 3af8494
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ Just register a new instance of <code>VavrModule</code>
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new VavrModule());
```

### Serialization/deserialization

<!-- see io.vavr.jackson.datatype.docs.ReadmeTest#testDeser -->

```java
String json = mapper.writer().writeValueAsString(List.of(List.of(1)));
// = [[1]]
Object restored1 = mapper.readValue(json, List.class);
// = List(java.util.ArrayList(1))
Object restored2 = mapper.readValue(json, new TypeReference<List<List<?>>>() {});
// = List(List(1))
String json = mapper.writeValueAsString(List.of(1));
// = [1]
List<Integer> restored = mapper.readValue(json, new TypeReference<List<Integer>>() {});
// = List(1)
```

## Using Developer Versions

Developer versions can be found [here](https://oss.sonatype.org/content/repositories/snapshots/io/vavr/vavr-jackson).
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/io/vavr/jackson/datatype/docs/ReadmeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.vavr.jackson.datatype.docs;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.vavr.collection.List;
import io.vavr.jackson.datatype.BaseTest;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class ReadmeTest extends BaseTest {

@Test
void testDeser() throws Exception {
ObjectMapper mapper = mapper();

// readme: Serialization/deserialization
String json = mapper.writeValueAsString(List.of(1));
List<Integer> restored = mapper.readValue(json, new TypeReference<List<Integer>>() {});
// end of readme

assertEquals("[1]", json);
assertEquals(List.of(1), restored);
assertEquals("List(1)", restored.toString());
}
}

0 comments on commit 3af8494

Please sign in to comment.