-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
serialization improvements: tests and interfaces
Signed-off-by: Erik Escher <eclipse@erikescher.de>
- Loading branch information
1 parent
72de344
commit e5fdb67
Showing
36 changed files
with
743 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
json/src/main/java/org/eclipse/ditto/json/ByteBufferOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (c) 2019 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
|
||
package org.eclipse.ditto.json; | ||
|
||
import java.io.OutputStream; | ||
import java.nio.ByteBuffer; | ||
|
||
public class ByteBufferOutputStream extends OutputStream { | ||
|
||
private final ByteBuffer destinationBuffer; | ||
|
||
ByteBufferOutputStream(ByteBuffer destinationBuffer){ | ||
this.destinationBuffer = destinationBuffer; | ||
} | ||
|
||
@Override | ||
public void write(final int b) { | ||
// super specifies to ignore everything except the lower 8 bits. | ||
destinationBuffer.put((byte) b); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
json/src/main/java/org/eclipse/ditto/json/CborFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright (c) 2019 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
|
||
package org.eclipse.ditto.json; | ||
|
||
import java.io.Reader; | ||
import java.nio.ByteBuffer; | ||
|
||
//TODO: move these Methods to JsonFactory | ||
|
||
public final class CborFactory { | ||
|
||
/* | ||
* This utility class is not meant to be instantiated. | ||
*/ | ||
private CborFactory(){ | ||
throw new AssertionError(); | ||
} | ||
|
||
public static JsonValue readFrom(final byte[] bytes){ | ||
// TODO implement | ||
return null; | ||
} | ||
|
||
public static JsonValue readFrom(final byte[] bytes, int offset, int length){ | ||
// TODO implement | ||
return null; | ||
} | ||
|
||
public static JsonValue readFrom(ByteBuffer byteBuffer){ | ||
// TODO implement | ||
return null; | ||
} | ||
|
||
public static JsonValue readFrom(Reader reader){ | ||
// TODO implement | ||
return null; | ||
} | ||
|
||
public static byte[] toByteArray(JsonValue jsonValue){ | ||
// TODO implement by calling jsonValue.toByteArray with an appropriate Generator | ||
return null; | ||
} | ||
|
||
public static ByteBuffer toByteBuffer(JsonValue jsonValue){ | ||
// TODO implement by calling jsonValue.toByteBuffer with an appropriate Generator | ||
return null; | ||
} | ||
|
||
// TODO: support the equivalent of Reader | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
json/src/main/java/org/eclipse/ditto/json/SerializationContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright (c) 2019 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
|
||
package org.eclipse.ditto.json; | ||
|
||
import java.io.Closeable; | ||
import java.io.Flushable; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.nio.ByteBuffer; | ||
|
||
import com.fasterxml.jackson.core.JsonFactory; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
|
||
/** | ||
* A class that bundles State and Configuration for serialization. | ||
* It must be recreated for each serialization target. | ||
*/ | ||
public class SerializationContext implements Closeable, Flushable { | ||
|
||
private JsonGenerator jacksonGenerator; | ||
|
||
/** | ||
* Creates a Serialization context that writes to the designated target. | ||
* @param jacksonFactory The JsonFactory (from Jackson) to use during serialization. It defines among other things the Format to use. | ||
* @param outputStream The stream to write serialized data to. The stream is considered to be borrowed and will not be closed. | ||
*/ | ||
public SerializationContext(JsonFactory jacksonFactory, OutputStream outputStream) throws IOException { | ||
jacksonGenerator = jacksonFactory.createGenerator(outputStream); | ||
} | ||
|
||
/** | ||
* Creates a Serialization context that writes to the designated target. | ||
* @param jacksonFactory The JsonFactory (from Jackson) to use during serialization. It defines among other things the Format to use. | ||
* @param targetBuffer The Buffer to write serialized data to. The Buffer is considered to be borrowed and will not be closed. | ||
*/ | ||
public SerializationContext(JsonFactory jacksonFactory, ByteBuffer targetBuffer) throws IOException { | ||
this(jacksonFactory, new ByteBufferOutputStream(targetBuffer)); | ||
} | ||
|
||
JsonGenerator getJacksonGenerator() { | ||
return jacksonGenerator; | ||
} | ||
|
||
/** | ||
* Closes internal objects that need to be closed. Targets will be closed according to the promise during creation. | ||
*/ | ||
@Override | ||
public void close() throws IOException { | ||
jacksonGenerator.close(); | ||
} | ||
|
||
/** | ||
* Flushes everything including targets. | ||
*/ | ||
@Override | ||
public void flush() throws IOException { | ||
jacksonGenerator.flush(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
json/src/test/java/org/eclipse/ditto/json/ByteBufferOutputStreamIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (c) 2019 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
|
||
package org.eclipse.ditto.json; | ||
|
||
import static org.eclipse.ditto.json.assertions.DittoJsonAssertions.assertThat; | ||
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
|
||
import org.junit.Test; | ||
|
||
public class ByteBufferOutputStreamIT { | ||
|
||
@Test | ||
public void writesThroughToBuffer() throws IOException { | ||
final int expectedCount = 11; | ||
final ByteBuffer buffer = ByteBuffer.allocate(expectedCount); | ||
final ByteBufferOutputStream outputStream = new ByteBufferOutputStream(buffer); | ||
|
||
|
||
for (int i = 0; i < 5; i++) { | ||
outputStream.write(i); | ||
} | ||
outputStream.write(new byte[]{7,8,9}); | ||
outputStream.write(new byte[]{10,11,12,13,14}, 1, 3); | ||
|
||
assertThat(buffer.position()).isEqualTo(expectedCount - 1); | ||
} | ||
} |
Oops, something went wrong.