| 1 |
package com.fasterxml.jackson.dataformat.smile; |
| 2 |
|
| 3 |
import com.fasterxml.jackson.databind.*; |
| 4 |
|
| 5 |
public class TestFormatDetection extends SmileTestBase |
| 6 |
{ |
| 7 |
static class POJO { |
| 8 |
public int id; |
| 9 |
public String name; |
| 10 |
|
| 11 |
public POJO() { } |
| 12 |
public POJO(int id, String name) |
| 13 |
{ |
| 14 |
this.id = id; |
| 15 |
this.name = name; |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
/* |
| 20 |
/********************************************************** |
| 21 |
/* Test methods |
| 22 |
/********************************************************** |
| 23 |
*/ |
| 24 |
|
| 25 |
public void testSimple() throws Exception |
| 26 |
{ |
| 27 |
final ObjectMapper mapper = new ObjectMapper(); |
| 28 |
final ObjectReader jsonReader = mapper.readerFor(POJO.class); |
| 29 |
final String JSON = "{\"name\":\"Bob\", \"id\":3}"; |
| 30 |
|
| 31 |
byte[] doc = _smileDoc(JSON, true); |
| 32 |
|
| 33 |
ObjectReader detecting = jsonReader.withFormatDetection(jsonReader, |
| 34 |
jsonReader.with(new SmileFactory())); |
| 35 |
POJO pojo = detecting.readValue(doc); |
| 36 |
assertEquals(3, pojo.id); |
| 37 |
assertEquals("Bob", pojo.name); |
| 38 |
|
| 39 |
// let's verify it also works for plain JSON... |
| 40 |
pojo = detecting.readValue(JSON.getBytes("UTF-8")); |
| 41 |
assertEquals(3, pojo.id); |
| 42 |
assertEquals("Bob", pojo.name); |
| 43 |
} |
| 44 |
} |