-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#72 First version for PlainByteArraySerializer/Deserializer. Modulbin…
…ding not yet implemented
- Loading branch information
Showing
5 changed files
with
92 additions
and
6 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
31 changes: 31 additions & 0 deletions
31
...c/main/java/de/ppi/deepsampler/persistence/json/extension/PlainByteArrayDeserializer.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,31 @@ | ||
package de.ppi.deepsampler.persistence.json.extension; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.core.ObjectCodec; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
|
||
import java.io.IOException; | ||
|
||
public class PlainByteArrayDeserializer extends JsonDeserializer<byte[]> { | ||
@Override | ||
public byte[] deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
|
||
ObjectCodec oc = p.getCodec(); | ||
JsonNode node = oc.readTree(p); | ||
|
||
if (node.isArray() && node.isEmpty()) { | ||
return null; | ||
} | ||
|
||
byte[] returnValue = new byte[node.size()]; | ||
int i =0; | ||
for (JsonNode value : node) { | ||
returnValue[i]= (byte) value.asInt(); | ||
i++; | ||
} | ||
|
||
return returnValue; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...src/main/java/de/ppi/deepsampler/persistence/json/extension/PlainByteArraySerializer.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,37 @@ | ||
package de.ppi.deepsampler.persistence.json.extension; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonToken; | ||
import com.fasterxml.jackson.core.type.WritableTypeId; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.jsontype.TypeSerializer; | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
|
||
import java.io.IOException; | ||
|
||
public class PlainByteArraySerializer extends StdSerializer<byte[]> { | ||
|
||
|
||
public PlainByteArraySerializer() { | ||
super(byte[].class); | ||
} | ||
|
||
@Override | ||
public void serialize(byte[] value, JsonGenerator gen, SerializerProvider serializers) throws IOException { | ||
gen.writeStartArray(); | ||
for (byte b: value) { | ||
gen.writeNumber(b ); | ||
} | ||
gen.writeEndArray(); | ||
} | ||
|
||
@Override | ||
public void serializeWithType(byte[] value, JsonGenerator gen, SerializerProvider serializers, TypeSerializer typeSer) throws IOException { | ||
// most likely scalar | ||
WritableTypeId typeIdDef = typeSer.writeTypePrefix(gen, | ||
typeSer.typeId(value, JsonToken.VALUE_EMBEDDED_OBJECT)); | ||
serialize(value,gen,serializers); | ||
typeSer.writeTypeSuffix(gen, typeIdDef); | ||
} | ||
} |
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