NodeMapper { public Node serialize(Object object) { return serialize(object, Collections.newSetFromMap(new IdentityHashMap<>())); }  void setSerializeNullValues(boolean serializeNullValues); boolean getSerializeNullValues(); void setWhenMissingSetter(WhenMissing whenMissing); WhenMissing getWhenMissingSetter(); void disableToNodeForClass(Class type); void enableToNodeForClass(Class type); Set<Class> getDisableToNode(); void disableFromNodeForClass(Class type); void enableFromNodeForClass(Class type); Set<Class> getDisableFromNode(); boolean getOmitEmptyValues(); void setOmitEmptyValues(boolean omitEmptyValues); Node serialize(Object object); T deserialize(Node value, Class<T> into); T deserializeInto(Node value, T objectToMutate); @SuppressWarnings("unchecked") V deserializeCollection(
            Node value,
            Class<T> into,
            Class<U> members
    ); @SuppressWarnings("unchecked") V deserializeMap(
            Node value, Class<T> into,
            Class<U> members
    );  }
@Test public void convertsComplexObjectsToNode() { List<Map<String, Foo>> value = new ArrayList<>(); Map<String, Foo> mappings = new LinkedHashMap<>(); mappings.put("hi", new Foo()); value.add(mappings); NodeMapper mapper = new NodeMapper(); Node result = mapper.serialize(value); Node.assertEquals(result, Node.parse("[ {\"hi\": { \"baz\": \"baz\", \"bar\": \"bar\" } } ]")); }
@Test public void serializesAlphabetically() { NodeMapper mapper = new NodeMapper(); ObjectNode result = mapper.serialize(new Foo()).expectObjectNode(); Collection<String> keys = result.getStringMap().keySet(); assertThat(keys, contains("bar", "baz")); }
@Test public void serializesCollectionsAndArrays() { NodeMapper mapper = new NodeMapper(); List<String> foos = ListUtils.of("a", "b", "c"); String[] foosArray = foos.toArray(new String[0]); Node result1 = mapper.serialize(foos); Node result2 = mapper.serialize(foosArray); assertThat(result1, equalTo(result2)); assertThat(result1, equalTo(Node.fromStrings("a", "b", "c"))); }
@Test public void doesNotSerializeTransientValues() { NodeMapper mapper = new NodeMapper(); HasTransient hasTransient = new HasTransient(); hasTransient.setFoo("foo"); hasTransient.setHi("hi"); ObjectNode result = mapper.serialize(hasTransient).expectObjectNode(); Node.assertEquals(result, Node.parse("{\"hi\": \"hi\"}")); }
@Test public void serializesUsingToNode() { NodeMapper mapper = new NodeMapper(); HasToNode value = new HasToNode(); Node result = mapper.serialize(value); Node.assertEquals(result, Node.parse("{\"foo\": \"baz\"}")); }
@Test public void serializesKitchenSink() { NodeMapper mapper = new NodeMapper(); Node result = mapper.serialize(new KitchenSink()); Node.assertEquals(result, Node.parse("{\n" + " \"long\": 4,\n" + " \"float\": 5.0,\n" + " \"string\": \"string\",\n" + " \"shapeId\": \"foo.baz#Bar\",\n" + " \"double\": 6.0,\n" + " \"boolean\": true,\n" + " \"enum\": \"array\",\n" + " \"int\": 3,\n" + " \"presentString\": \"present\",\n" + " \"short\": 2,\n" + " \"boolean2\": true,\n" + " \"byte\": 1,\n" + " \"map\": {\n" + " \"b\": [\n" + " \"2\",\n" + " \"3\"\n" + " ],\n" + " \"a\": [\n" + " \"1\",\n" + " \"2\"\n" + " ]\n" + " },\n" + " \"complexList\": [\n" + " [\n" + " \"a\",\n" + " \"b\"\n" + " ],\n" + " [\n" + " \"c\",\n" + " \"d\"\n" + " ]\n" + " ],\n" + " \"falseBoolean\": false,\n" + " \"stringIterable\": [\n" + " \"foo\",\n" + " \"baz\"\n" + " ]\n" + "}")); }
@Test public void doesNotInfinitelySerializeSelfReferentialTypes() { NodeMapper mapper = new NodeMapper(); SelfReferential value = new SelfReferential(); value.recursive = value; Node result = mapper.serialize(value); Node.assertEquals(result, Node.parse("{\"name\": \"hi\"}")); }
@Test public void serializesRepeatedButNotRecursiveValues() { RepeatedValues a = new RepeatedValues(); List<RepeatedValues> repeatedValues = ListUtils.of(a, a, a); NodeMapper mapper = new NodeMapper(); Node result = mapper.serialize(repeatedValues); Node.assertEquals(result, Node.parse("[ {\"foo\": \"foo\"}, {\"foo\": \"foo\"}, {\"foo\": \"foo\"} ]")); }
@Test public void doesNotSerializeLotsOfStuff() { NodeMapper mapper = new NodeMapper(); NonSerialized value = new NonSerialized(); Node result = mapper.serialize(value); Node.assertEquals(result, Node.objectNode()); }
@Test public void cannotSerializeMapsWhenKeyIsNotString() { Map<List<String>, String> badMap = new HashMap<>(); badMap.put(ListUtils.of("a"), "a"); NodeMapper mapper = new NodeMapper(); Assertions.assertThrows(NodeSerializationException.class, () -> { mapper.serialize(badMap); }); }
@Test public void catchesAndShowsErrorsWhenSerializing() { ThrowingClass tc = new ThrowingClass(); NodeMapper mapper = new NodeMapper(); NodeSerializationException e = Assertions.assertThrows( NodeSerializationException.class, () -> mapper.serialize(tc) ); assertThat(e.getMessage(), equalTo( "Error serializing `throws` field of software.amazon.smithy.model.node.NodeMapperTest$ThrowingClass " + "using getThrows(): Nope!")); }
@Test public void doesNotSerializeSourceLocation() { NodeMapper mapper = new NodeMapper(); HasSourceLocation hs = new HasSourceLocation(); hs.setSourceLocation(new SourceLocation("/foo/baz")); hs.setFoo("hi"); Node result = mapper.serialize(hs); assertThat(result.expectObjectNode().getStringMap(), hasKey("foo")); assertThat(result.expectObjectNode().getStringMap(), not(hasKey("sourceLocation"))); }