Skip to content

Commit

Permalink
Introduce generic JsonException & make LwM2mJsonException a checked one
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Jan 3, 2020
1 parent 3efc58b commit d96a665
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.eclipse.leshan.json.JsonArrayEntry;
import org.eclipse.leshan.json.JsonRootObject;
import org.eclipse.leshan.json.LwM2mJson;
import org.eclipse.leshan.json.LwM2mJsonException;
import org.eclipse.leshan.util.Base64;
import org.eclipse.leshan.util.Validate;
import org.slf4j.Logger;
Expand All @@ -59,7 +60,11 @@ public static byte[] encode(LwM2mNode node, LwM2mPath path, LwM2mModel model, Lw
JsonRootObject jsonObject = new JsonRootObject();
jsonObject.setResourceList(internalEncoder.resourceList);
jsonObject.setBaseName(internalEncoder.baseName);
return LwM2mJson.toJsonLwM2m(jsonObject).getBytes();
try {
return LwM2mJson.toJsonLwM2m(jsonObject).getBytes();
} catch (LwM2mJsonException e) {
throw new CodecException(e, "Unable to encode node[path:%s] : %s", path, node);
}
}

public static byte[] encodeTimestampedData(List<TimestampedLwM2mNode> timestampedNodes, LwM2mPath path,
Expand Down Expand Up @@ -93,7 +98,12 @@ public static byte[] encodeTimestampedData(List<TimestampedLwM2mNode> timestampe
JsonRootObject jsonObject = new JsonRootObject();
jsonObject.setResourceList(entries);
jsonObject.setBaseName(internalEncoder.baseName);
return LwM2mJson.toJsonLwM2m(jsonObject).getBytes();
try {
return LwM2mJson.toJsonLwM2m(jsonObject).getBytes();
} catch (LwM2mJsonException e) {
throw new CodecException(e, "Unable to encode timestamped nodes[path:%s] : %s", path, timestampedNodes);
}

}

private static class InternalEncoder implements LwM2mNodeVisitor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.eclipse.leshan.json;

import org.eclipse.leshan.core.model.ResourceModel.Type;
import org.eclipse.leshan.util.json.JsonException;
import org.eclipse.leshan.util.json.JsonSerDes;

import com.eclipsesource.json.JsonObject;
Expand All @@ -24,7 +25,7 @@
public class JsonArrayEntrySerDes extends JsonSerDes<JsonArrayEntry> {

@Override
public JsonObject jSerialize(JsonArrayEntry jae) {
public JsonObject jSerialize(JsonArrayEntry jae) throws JsonException {
JsonObject o = new JsonObject();
if (jae.getName() != null)
o.add("n", jae.getName());
Expand All @@ -44,7 +45,7 @@ public JsonObject jSerialize(JsonArrayEntry jae) {
o.add("sv", jae.getStringValue());
break;
default:
throw new LwM2mJsonException("JsonArrayEntry MUST have a value : %s", jae);
throw new JsonException("JsonArrayEntry MUST have a value : %s", jae);
}
}
if (jae.getTime() != null)
Expand All @@ -53,7 +54,7 @@ public JsonObject jSerialize(JsonArrayEntry jae) {
};

@Override
public JsonArrayEntry deserialize(JsonObject o) {
public JsonArrayEntry deserialize(JsonObject o) throws JsonException {
if (o == null)
return null;

Expand Down Expand Up @@ -81,7 +82,7 @@ public JsonArrayEntry deserialize(JsonObject o) {
jae.setObjectLinkValue(ov.asString());

if (jae.getType() == null) {
throw new LwM2mJsonException("Missing value(v,bv,ov,sv) field for entry %s", o.toString());
throw new JsonException("Missing value(v,bv,ov,sv) field for entry %s", o.toString());
}

return jae;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*******************************************************************************/
package org.eclipse.leshan.json;

import org.eclipse.leshan.util.json.JsonException;
import org.eclipse.leshan.util.json.JsonSerDes;

import com.eclipsesource.json.JsonArray;
Expand All @@ -26,7 +27,7 @@ public class JsonRootObjectSerDes extends JsonSerDes<JsonRootObject> {
private JsonArrayEntrySerDes serDes = new JsonArrayEntrySerDes();

@Override
public JsonObject jSerialize(JsonRootObject jro) {
public JsonObject jSerialize(JsonRootObject jro) throws JsonException {
JsonObject o = new JsonObject();

if (jro.getBaseName() != null)
Expand All @@ -43,7 +44,7 @@ public JsonObject jSerialize(JsonRootObject jro) {
};

@Override
public JsonRootObject deserialize(JsonObject o) {
public JsonRootObject deserialize(JsonObject o) throws JsonException {
if (o == null)
return null;

Expand All @@ -53,7 +54,7 @@ public JsonRootObject deserialize(JsonObject o) {
if (e != null)
jro.setResourceList(serDes.deserialize(e.asArray()));
else
throw new LwM2mJsonException("'e' field is missing for %s", o.toString());
throw new JsonException("'e' field is missing for %s", o.toString());

JsonValue bn = o.get("bn");
if (bn != null && bn.isString())
Expand Down
17 changes: 14 additions & 3 deletions leshan-core/src/main/java/org/eclipse/leshan/json/LwM2mJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

package org.eclipse.leshan.json;

import org.eclipse.leshan.util.json.JsonException;

import com.eclipsesource.json.Json;
import com.eclipsesource.json.ParseException;

/**
* Helper for encoding/decoding LWM2M JSON format
Expand All @@ -26,11 +29,19 @@ public class LwM2mJson {

private static final JsonRootObjectSerDes serDes = new JsonRootObjectSerDes();

public static String toJsonLwM2m(JsonRootObject jro) {
return serDes.sSerialize(jro);
public static String toJsonLwM2m(JsonRootObject jro) throws LwM2mJsonException {
try {
return serDes.sSerialize(jro);
} catch (JsonException e) {
throw new LwM2mJsonException("Unable to serialize LWM2M JSON.", e);
}
}

public static JsonRootObject fromJsonLwM2m(String jsonString) throws LwM2mJsonException {
return serDes.deserialize(Json.parse(jsonString).asObject());
try {
return serDes.deserialize(Json.parse(jsonString).asObject());
} catch (JsonException | ParseException e) {
throw new LwM2mJsonException("Unable to parse LWM2M JSON.", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
/**
* Exception thrown in case of JSON parsing error
*/
public class LwM2mJsonException extends RuntimeException {
public class LwM2mJsonException extends Exception {

private static final long serialVersionUID = 1L;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*******************************************************************************
* Copyright (c) 2020 Sierra Wireless and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Sierra Wireless - initial API and implementation
*******************************************************************************/
package org.eclipse.leshan.util.json;

/**
* Exception raised by concrete implementation of {@link JsonSerDes} during serialization and deserialization.
*/
public class JsonException extends RuntimeException {

private static final long serialVersionUID = 1L;

public JsonException(String message) {
super(message);
}

public JsonException(String message, Object... args) {
super(String.format(message, args));
}

public JsonException(Exception e, String message, Object... args) {
super(String.format(message, args), e);
}

public JsonException(String message, Exception cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,24 @@
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;

/**
* An abstract class to easily create serializer/deserializer class based on "minimal-json".
*
* @param <T>
*/
public abstract class JsonSerDes<T> {

public abstract JsonObject jSerialize(T t);
public abstract JsonObject jSerialize(T t) throws JsonException;

public String sSerialize(T t) {
public String sSerialize(T t) throws JsonException {
return jSerialize(t).toString();
}

public byte[] bSerialize(T t) {
public byte[] bSerialize(T t) throws JsonException {
return jSerialize(t).toString().getBytes();
}

public JsonArray jSerialize(Collection<T> ts) {
public JsonArray jSerialize(Collection<T> ts) throws JsonException {
JsonArray o = new JsonArray();
if (ts != null) {
for (T t : ts) {
Expand All @@ -47,21 +52,21 @@ public JsonArray jSerialize(Collection<T> ts) {
return o;
}

public String sSerialize(Collection<T> ts) {
public String sSerialize(Collection<T> ts) throws JsonException {
return jSerialize(ts).toString();
}

public byte[] bSerialize(Collection<T> ts) {
public byte[] bSerialize(Collection<T> ts) throws JsonException {
return jSerialize(ts).toString().getBytes();
}

public abstract T deserialize(JsonObject o);
public abstract T deserialize(JsonObject o) throws JsonException;

public List<T> deserialize(JsonArray a) {
public List<T> deserialize(JsonArray a) throws JsonException {
ArrayList<T> res = new ArrayList<>(a.size());
for (JsonValue v : a) {
res.add(deserialize(v.asObject()));
}
return res;
}
}
}

0 comments on commit d96a665

Please sign in to comment.