|
| 1 | +/* |
| 2 | + * Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at: |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0/ |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific |
| 12 | + * language governing permissions and limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.fasterxml.jackson.dataformat.ion; |
| 16 | + |
| 17 | +import com.amazon.ion.IonSexp; |
| 18 | +import com.amazon.ion.IonSystem; |
| 19 | +import com.amazon.ion.IonWriter; |
| 20 | +import com.amazon.ion.system.IonSystemBuilder; |
| 21 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 22 | +import com.fasterxml.jackson.databind.JsonSerializer; |
| 23 | +import com.fasterxml.jackson.databind.SerializerProvider; |
| 24 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
| 25 | +import java.io.ByteArrayOutputStream; |
| 26 | +import java.io.IOException; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.List; |
| 29 | +import org.junit.Assert; |
| 30 | +import org.junit.Before; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +/** |
| 34 | + * End to end test verifying we can serialize sexps |
| 35 | + */ |
| 36 | +public class GenerateSexpTest { |
| 37 | + |
| 38 | + private IonSystem ionSystem; |
| 39 | + private IonObjectMapper mapper; |
| 40 | + |
| 41 | + @Before |
| 42 | + public void setup() { |
| 43 | + this.ionSystem = IonSystemBuilder.standard().build(); |
| 44 | + this.mapper = new IonObjectMapper(new IonFactory(null, ionSystem)); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void topLevel() throws IOException { |
| 49 | + Assert.assertEquals( |
| 50 | + ionSystem.singleValue("(foo \"bar\")"), |
| 51 | + mapper.writeValueAsIonValue(new SexpObject("foo", "bar"))); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void inList() throws IOException { |
| 56 | + Assert.assertEquals( |
| 57 | + ionSystem.singleValue("[(foo \"bar\"), (baz \"qux\")]"), |
| 58 | + mapper.writeValueAsIonValue( |
| 59 | + Arrays.asList(new SexpObject("foo", "bar"), new SexpObject("baz", "qux")))); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void inObject() throws IOException { |
| 64 | + Assert.assertEquals( |
| 65 | + ionSystem.singleValue("{sexpField:(foo \"bar\")}"), |
| 66 | + mapper.writeValueAsIonValue(new SexpObjectContainer(new SexpObject("foo", "bar")))); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void inOtherSexp() throws IOException { |
| 71 | + Assert.assertEquals( |
| 72 | + ionSystem.singleValue("(foo (bar \"baz\"))"), |
| 73 | + mapper.writeValueAsIonValue(new SexpObject("foo", new SexpObject("bar", "baz")))); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void generatorUsedInStreamingWriteText() throws IOException { |
| 78 | + Assert.assertArrayEquals("(foo 0)".getBytes(), toBytes(new SexpObject("foo", 0), mapper)); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void generatorUsedInStreamingWriteBinary() throws IOException { |
| 83 | + byte[] expectedBytes = null; |
| 84 | + |
| 85 | + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 86 | + IonWriter writer = ionSystem.newBinaryWriter(baos)) { |
| 87 | + ionSystem.singleValue("(foo 0)").writeTo(writer); |
| 88 | + writer.finish(); |
| 89 | + expectedBytes = baos.toByteArray(); |
| 90 | + } |
| 91 | + |
| 92 | + mapper.setCreateBinaryWriters(true); |
| 93 | + Assert.assertArrayEquals(expectedBytes, toBytes(new SexpObject("foo", 0), mapper)); |
| 94 | + } |
| 95 | + |
| 96 | + private byte[] toBytes(Object object, IonObjectMapper mapper) throws IOException { |
| 97 | + byte[] bytes = null; |
| 98 | + |
| 99 | + try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { |
| 100 | + mapper.writeValue(baos, object); |
| 101 | + bytes = baos.toByteArray(); |
| 102 | + } |
| 103 | + |
| 104 | + return bytes; |
| 105 | + } |
| 106 | + |
| 107 | + private static class SexpObjectContainer { |
| 108 | + private SexpObject sexpField; |
| 109 | + |
| 110 | + SexpObjectContainer(SexpObject sexpField) { |
| 111 | + this.sexpField = sexpField; |
| 112 | + } |
| 113 | + |
| 114 | + public SexpObject getSexpField() { |
| 115 | + return sexpField; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // Create some pojo that defines a custom serializer that creates an IonSexp |
| 120 | + @JsonSerialize(using=SexpObjectSerializer.class) |
| 121 | + private static class SexpObject { |
| 122 | + private String symbolField; |
| 123 | + private Object objectField; |
| 124 | + |
| 125 | + SexpObject(String symbolField, Object objectField) { |
| 126 | + this.symbolField = symbolField; |
| 127 | + this.objectField = objectField; |
| 128 | + } |
| 129 | + |
| 130 | + public String getSymbolField() { |
| 131 | + return symbolField; |
| 132 | + } |
| 133 | + |
| 134 | + public Object getObjectField() { |
| 135 | + return objectField; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private static class SexpObjectSerializer extends JsonSerializer<SexpObject> { |
| 140 | + @Override |
| 141 | + public void serialize(SexpObject value, JsonGenerator jsonGenerator, |
| 142 | + SerializerProvider provider) throws IOException { |
| 143 | + final IonGenerator ionGenerator = (IonGenerator) jsonGenerator; |
| 144 | + |
| 145 | + ionGenerator.writeStartSexp(); |
| 146 | + ionGenerator.writeSymbol(value.getSymbolField()); |
| 147 | + ionGenerator.writeObject(value.getObjectField()); |
| 148 | + ionGenerator.writeEndSexp(); |
| 149 | + } |
| 150 | + } |
| 151 | +} |
0 commit comments