|
| 1 | +package com.baeldung.jsoniter; |
| 2 | + |
| 3 | +import com.baeldung.jsoniter.model.Name; |
| 4 | +import com.baeldung.jsoniter.model.Student; |
| 5 | +import com.jsoniter.JsonIterator; |
| 6 | +import com.jsoniter.ValueType; |
| 7 | +import com.jsoniter.any.Any; |
| 8 | + |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import static com.jsoniter.ValueType.STRING; |
| 12 | +import static org.assertj.core.api.Assertions.assertThat; |
| 13 | + |
| 14 | +public class JsoniterIntroUnitTest { |
| 15 | + |
| 16 | + @Test |
| 17 | + public void whenParsedUsingBindAPI_thenConvertedToJavaObjectCorrectly() { |
| 18 | + String input = "{\"id\":1,\"name\":{\"firstName\":\"Joe\",\"surname\":\"Blogg\"}}"; |
| 19 | + |
| 20 | + Student student = JsonIterator.deserialize(input, Student.class); |
| 21 | + |
| 22 | + assertThat(student.getId()).isEqualTo(1); |
| 23 | + assertThat(student.getName().getFirstName()).isEqualTo("Joe"); |
| 24 | + assertThat(student.getName().getSurname()).isEqualTo("Blogg"); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void givenTypeInJsonFuzzy_whenFieldIsMaybeDecoded_thenFieldParsedCorrectly() { |
| 29 | + String input = "{\"id\":\"1\",\"name\":{\"firstName\":\"Joe\",\"surname\":\"Blogg\"}}"; |
| 30 | + |
| 31 | + Student student = JsonIterator.deserialize(input, Student.class); |
| 32 | + |
| 33 | + assertThat(student.getId()).isEqualTo(1); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void whenParsedUsingAnyAPI_thenFieldValueCanBeExtractedUsingTheFieldName() { |
| 38 | + String input = "{\"id\":1,\"name\":{\"firstName\":\"Joe\",\"surname\":\"Blogg\"}}"; |
| 39 | + |
| 40 | + Any any = JsonIterator.deserialize(input); |
| 41 | + |
| 42 | + assertThat(any.toInt("id")).isEqualTo(1); |
| 43 | + assertThat(any.toString("name", "firstName")).isEqualTo("Joe"); |
| 44 | + assertThat(any.toString("name", "surname")).isEqualTo("Blogg"); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void whenParsedUsingAnyAPI_thenFieldValueTypeIsCorrect() { |
| 49 | + String input = "{\"id\":1,\"name\":{\"firstName\":\"Joe\",\"surname\":\"Blogg\"}}"; |
| 50 | + |
| 51 | + Any any = JsonIterator.deserialize(input); |
| 52 | + |
| 53 | + assertThat(any.get("id").valueType()).isEqualTo(ValueType.NUMBER); |
| 54 | + assertThat(any.get("name").valueType()).isEqualTo(ValueType.OBJECT); |
| 55 | + assertThat(any.get("error").valueType()).isEqualTo(ValueType.INVALID); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void whenParsedUsingIteratorAPI_thenFieldValuesExtractedCorrectly() throws Exception { |
| 60 | + Name name = new Name(); |
| 61 | + String input = "{ \"firstName\" : \"Joe\", \"surname\" : \"Blogg\" }"; |
| 62 | + JsonIterator iterator = JsonIterator.parse(input); |
| 63 | + |
| 64 | + for (String field = iterator.readObject(); field != null; field = iterator.readObject()) { |
| 65 | + switch (field) { |
| 66 | + case "firstName": |
| 67 | + if (iterator.whatIsNext() == ValueType.STRING) { |
| 68 | + name.setFirstName(iterator.readString()); |
| 69 | + } |
| 70 | + continue; |
| 71 | + case "surname": |
| 72 | + if (iterator.whatIsNext() == ValueType.STRING) { |
| 73 | + name.setSurname(iterator.readString()); |
| 74 | + } |
| 75 | + continue; |
| 76 | + default: |
| 77 | + iterator.skip(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + assertThat(name.getFirstName()).isEqualTo("Joe"); |
| 82 | + assertThat(name.getSurname()).isEqualTo("Blogg"); |
| 83 | + } |
| 84 | + |
| 85 | +} |
0 commit comments