Search in sources :

Example 36 with JsonValue

use of com.eclipsesource.json.JsonValue in project box-android-sdk by box.

the class BoxEntity method createEntityFromJson.

/**
 * Helper method that will parse into a known child of BoxEntity.
 *
 * @param json JsonObject representing a BoxEntity or one of its known children.
 * @return a BoxEntity or one of its known children.
 */
public static BoxEntity createEntityFromJson(final JsonObject json) {
    JsonValue typeValue = json.get(BoxEntity.FIELD_TYPE);
    if (!typeValue.isString()) {
        return null;
    }
    String type = typeValue.asString();
    BoxEntityCreator creator = ENTITY_ADDON_MAP.get(type);
    BoxEntity entity = null;
    if (creator == null) {
        entity = new BoxEntity();
    } else {
        entity = creator.createEntity();
    }
    entity.createFromJson(json);
    return entity;
}
Also used : JsonValue(com.eclipsesource.json.JsonValue)

Example 37 with JsonValue

use of com.eclipsesource.json.JsonValue in project leshan by eclipse.

the class ObjectModelSerDesTest method des_ser_must_be_equals.

@Test
public void des_ser_must_be_equals() throws IOException {
    // load file
    InputStream inputStream = ObjectModelSerDesTest.class.getResourceAsStream("/model.json");
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
    while ((length = inputStream.read(buffer)) != -1) {
        result.write(buffer, 0, length);
    }
    String smodel = result.toString("UTF-8");
    // deserialize
    ObjectModelSerDes serDes = new ObjectModelSerDes();
    JsonValue json = Json.parse(smodel);
    List<ObjectModel> models = serDes.deserialize(json.asArray());
    // serialize
    JsonArray arr = serDes.jSerialize(models);
    String res = arr.toString(WriterConfig.PRETTY_PRINT);
    Assert.assertEquals("value should be equals", smodel, res);
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) ObjectModel(org.eclipse.leshan.core.model.ObjectModel) InputStream(java.io.InputStream) JsonValue(com.eclipsesource.json.JsonValue) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectModelSerDes(org.eclipse.leshan.core.model.json.ObjectModelSerDes) Test(org.junit.Test)

Example 38 with JsonValue

use of com.eclipsesource.json.JsonValue in project leshan by eclipse.

the class JsonArrayEntrySerDes method deserialize.

@Override
public JsonArrayEntry deserialize(JsonObject o) {
    if (o == null)
        return null;
    JsonArrayEntry jae = new JsonArrayEntry();
    jae.setName(o.getString("n", null));
    JsonValue t = o.get("t");
    if (t != null && t.isNumber())
        jae.setTime(t.asLong());
    JsonValue v = o.get("v");
    if (v != null && v.isNumber())
        jae.setFloatValue(v.asDouble());
    JsonValue bv = o.get("bv");
    if (bv != null && bv.isBoolean())
        jae.setBooleanValue(bv.asBoolean());
    JsonValue sv = o.get("sv");
    if (sv != null && sv.isString())
        jae.setStringValue(sv.asString());
    JsonValue ov = o.get("ov");
    if (ov != null && ov.isString())
        jae.setObjectLinkValue(ov.asString());
    return jae;
}
Also used : JsonValue(com.eclipsesource.json.JsonValue)

Example 39 with JsonValue

use of com.eclipsesource.json.JsonValue in project leshan by eclipse.

the class JsonRootObjectSerDes method deserialize.

@Override
public JsonRootObject deserialize(JsonObject o) {
    if (o == null)
        return null;
    JsonRootObject jro = new JsonRootObject();
    JsonValue e = o.get("e");
    if (e != null)
        jro.setResourceList(serDes.deserialize(e.asArray()));
    JsonValue bn = o.get("bn");
    if (bn != null && bn.isString())
        jro.setBaseName(bn.asString());
    JsonValue bt = o.get("bt");
    if (bt != null && bt.isNumber())
        jro.setBaseTime(bt.asLong());
    return jro;
}
Also used : JsonValue(com.eclipsesource.json.JsonValue)

Example 40 with JsonValue

use of com.eclipsesource.json.JsonValue in project leshan by eclipse.

the class EndpointContextSerDes method deserialize.

public static EndpointContext deserialize(JsonObject peer) {
    String address = peer.get(KEY_ADDRESS).asString();
    int port = peer.get(KEY_PORT).asInt();
    InetSocketAddress socketAddress = new InetSocketAddress(address, port);
    Principal principal = null;
    JsonValue value = peer.get(KEY_ID);
    if (value != null) {
        principal = new PreSharedKeyIdentity(value.asString());
    } else if ((value = peer.get(KEY_RPK)) != null) {
        try {
            byte[] rpk = Hex.decodeHex(value.asString().toCharArray());
            X509EncodedKeySpec spec = new X509EncodedKeySpec(rpk);
            PublicKey publicKey = KeyFactory.getInstance("EC").generatePublic(spec);
            principal = new RawPublicKeyIdentity(publicKey);
        } catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
            throw new IllegalStateException("Invalid security info content", e);
        }
    } else if ((value = peer.get(KEY_DN)) != null) {
        principal = new X500Principal(value.asString());
    }
    EndpointContext endpointContext;
    value = peer.get(KEY_ATTRIBUTES);
    if (value == null) {
        endpointContext = new AddressEndpointContext(socketAddress, principal);
    } else {
        int index = 0;
        String[] attributes = new String[value.asObject().size() * 2];
        for (Member member : value.asObject()) {
            attributes[index++] = member.getName();
            attributes[index++] = member.getValue().asString();
        }
        endpointContext = new MapBasedEndpointContext(socketAddress, principal, attributes);
    }
    return endpointContext;
}
Also used : AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) MapBasedEndpointContext(org.eclipse.californium.elements.MapBasedEndpointContext) EndpointContext(org.eclipse.californium.elements.EndpointContext) InetSocketAddress(java.net.InetSocketAddress) PublicKey(java.security.PublicKey) RawPublicKeyIdentity(org.eclipse.californium.elements.auth.RawPublicKeyIdentity) JsonValue(com.eclipsesource.json.JsonValue) AddressEndpointContext(org.eclipse.californium.elements.AddressEndpointContext) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) MapBasedEndpointContext(org.eclipse.californium.elements.MapBasedEndpointContext) X500Principal(javax.security.auth.x500.X500Principal) PreSharedKeyIdentity(org.eclipse.californium.elements.auth.PreSharedKeyIdentity) Member(com.eclipsesource.json.JsonObject.Member) X500Principal(javax.security.auth.x500.X500Principal) Principal(java.security.Principal)

Aggregations

JsonValue (com.eclipsesource.json.JsonValue)43 JsonObject (com.eclipsesource.json.JsonObject)19 JsonArray (com.eclipsesource.json.JsonArray)12 HashMap (java.util.HashMap)6 ParseException (com.eclipsesource.json.ParseException)4 IOException (java.io.IOException)4 InetSocketAddress (java.net.InetSocketAddress)3 Member (com.eclipsesource.json.JsonObject.Member)2 JsonUtil.getString (com.hazelcast.util.JsonUtil.getString)2 WalletCallException (com.vaklinov.zcashui.ZCashClientCaller.WalletCallException)2 FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 Reader (java.io.Reader)2 PublicKey (java.security.PublicKey)2 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Map (java.util.Map)2 EndpointContext (org.eclipse.californium.elements.EndpointContext)2 Link (org.eclipse.leshan.Link)2