-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Michał Wadowski <Michal.Wadowski@orange.com> Also-by: Simon Bernard <sbernard@sierrawireless.com>
- Loading branch information
1 parent
d2aed76
commit 3d2b1f9
Showing
21 changed files
with
769 additions
and
480 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
53 changes: 53 additions & 0 deletions
53
...demo/src/main/java/org/eclipse/leshan/server/bootstrap/demo/json/ByteArraySerializer.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,53 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Orange. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* and Eclipse Distribution License v1.0 which accompany this distribution. | ||
* | ||
* The Eclipse Public License is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* and the Eclipse Distribution License is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.html. | ||
* | ||
* Contributors: | ||
* Orange - keep one JSON dependency | ||
*******************************************************************************/ | ||
package org.eclipse.leshan.server.bootstrap.demo.json; | ||
|
||
import java.io.IOException; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
|
||
public class ByteArraySerializer extends StdSerializer<byte[]> { | ||
|
||
private static final long serialVersionUID = 1166117057647546937L; | ||
|
||
private final ByteMode byteMode; | ||
|
||
public enum ByteMode { | ||
SIGNED, UNSIGNED | ||
} | ||
|
||
protected ByteArraySerializer(Class<byte[]> t, ByteMode byteMode) { | ||
super(t); | ||
this.byteMode = byteMode; | ||
} | ||
|
||
public ByteArraySerializer(ByteMode byteMode) { | ||
this(byte[].class, byteMode); | ||
} | ||
|
||
@Override | ||
public void serialize(byte[] value, JsonGenerator gen, SerializerProvider provider) throws IOException { | ||
int[] output = new int[value.length]; | ||
|
||
int mask = byteMode == ByteMode.SIGNED ? -1 : 0xff; | ||
for (int i = 0; i < value.length; i++) { | ||
output[i] = value[i] & mask; | ||
} | ||
gen.writeArray(output, 0, output.length); | ||
} | ||
} |
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
67 changes: 67 additions & 0 deletions
67
...demo/src/main/java/org/eclipse/leshan/server/bootstrap/demo/json/EnumSetDeserializer.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,67 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Orange. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* and Eclipse Distribution License v1.0 which accompany this distribution. | ||
* | ||
* The Eclipse Public License is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* and the Eclipse Distribution License is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.html. | ||
* | ||
* Contributors: | ||
* Orange - keep one JSON dependency | ||
*******************************************************************************/ | ||
package org.eclipse.leshan.server.bootstrap.demo.json; | ||
|
||
import java.util.EnumSet; | ||
|
||
import org.eclipse.leshan.core.request.BindingMode; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.BeanProperty; | ||
import com.fasterxml.jackson.databind.DeserializationConfig; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.MapperFeature; | ||
import com.fasterxml.jackson.databind.deser.ContextualDeserializer; | ||
import com.fasterxml.jackson.databind.deser.std.EnumDeserializer; | ||
import com.fasterxml.jackson.databind.util.EnumResolver; | ||
|
||
public class EnumSetDeserializer extends JsonDeserializer<EnumSet<?>> implements ContextualDeserializer { | ||
|
||
@Override | ||
public EnumSet<BindingMode> deserialize(JsonParser p, DeserializationContext ctxt) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public JsonDeserializer<?> createContextual(DeserializationContext ctxt, BeanProperty property) { | ||
if (ctxt.getContextualType().getContentType().getRawClass() == BindingMode.class) { | ||
return new EnumSetBindingModeDeserializer(); | ||
} else { | ||
return buildDefaultEnumSetDeserializer(ctxt); | ||
} | ||
} | ||
|
||
private com.fasterxml.jackson.databind.deser.std.EnumSetDeserializer buildDefaultEnumSetDeserializer( | ||
DeserializationContext ctxt) { | ||
JavaType contextualType = ctxt.getContextualType(); | ||
JavaType contentType = contextualType.getContentType(); | ||
DeserializationConfig config = ctxt.getConfig(); | ||
|
||
return new com.fasterxml.jackson.databind.deser.std.EnumSetDeserializer(contentType, new EnumDeserializer( | ||
buildDefaultEnumResolver(ctxt), config.isEnabled(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS))); | ||
} | ||
|
||
protected EnumResolver buildDefaultEnumResolver(DeserializationContext ctxt) { | ||
JavaType contextualType = ctxt.getContextualType(); | ||
JavaType contentType = contextualType.getContentType(); | ||
Class<?> rawClass = contentType.getRawClass(); | ||
DeserializationConfig config = ctxt.getConfig(); | ||
|
||
return EnumResolver.constructFor(config, rawClass); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...r-demo/src/main/java/org/eclipse/leshan/server/bootstrap/demo/json/EnumSetSerializer.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,74 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2021 Orange. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* and Eclipse Distribution License v1.0 which accompany this distribution. | ||
* | ||
* The Eclipse Public License is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* and the Eclipse Distribution License is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.html. | ||
* | ||
* Contributors: | ||
* Orange - keep one JSON dependency | ||
*******************************************************************************/ | ||
package org.eclipse.leshan.server.bootstrap.demo.json; | ||
|
||
import java.io.IOException; | ||
import java.util.EnumSet; | ||
|
||
import org.eclipse.leshan.core.request.BindingMode; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.BeanDescription; | ||
import com.fasterxml.jackson.databind.JavaType; | ||
import com.fasterxml.jackson.databind.PropertyName; | ||
import com.fasterxml.jackson.databind.SerializationConfig; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.introspect.BasicBeanDescription; | ||
import com.fasterxml.jackson.databind.ser.std.StdSerializer; | ||
|
||
public class EnumSetSerializer extends StdSerializer<EnumSet<?>> { | ||
|
||
private static final long serialVersionUID = 7456682518094775441L; | ||
|
||
public EnumSetSerializer(JavaType type) { | ||
super(type); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public void serialize(EnumSet<?> value, JsonGenerator gen, SerializerProvider serializers) throws IOException { | ||
if (value == null) { | ||
gen.writeNull(); | ||
} else { | ||
if (isBindingMode(gen, serializers)) { | ||
gen.writeString(BindingMode.toString((EnumSet<BindingMode>) value)); | ||
} else { | ||
defaultEnumSetSerializer(value, gen, serializers); | ||
} | ||
} | ||
} | ||
|
||
private void defaultEnumSetSerializer(EnumSet<?> value, JsonGenerator gen, SerializerProvider serializers) | ||
throws IOException { | ||
JavaType valueType = serializers.getConfig().constructType(value.getClass()); | ||
com.fasterxml.jackson.databind.ser.std.EnumSetSerializer enumSetSerializer = new com.fasterxml.jackson.databind.ser.std.EnumSetSerializer( | ||
valueType); | ||
enumSetSerializer.serialize(value, gen, serializers); | ||
} | ||
|
||
private boolean isBindingMode(JsonGenerator gen, SerializerProvider serializers) { | ||
SerializationConfig config = serializers.getConfig(); | ||
|
||
JavaType currValue = config.constructType(gen.getCurrentValue().getClass()); | ||
BeanDescription beanDesc = config.introspect(currValue); | ||
|
||
String currentName = gen.getOutputContext().getCurrentName(); | ||
JavaType primaryType = ((BasicBeanDescription) beanDesc).findProperty(new PropertyName(currentName)) | ||
.getPrimaryType(); | ||
|
||
return primaryType.hasContentType() && primaryType.getContentType().getRawClass() == BindingMode.class; | ||
} | ||
} |
Oops, something went wrong.