Search in sources :

Example 11 with JsonArray

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

the class LwM2mNodeSerDes method deserialize.

public static LwM2mNode deserialize(JsonObject o) {
    String kind = o.getString("kind", null);
    int id = o.getInt("id", LwM2mObjectInstance.UNDEFINED);
    switch(kind) {
        case "object":
            {
                Collection<LwM2mObjectInstance> instances = new ArrayList<>();
                JsonArray jInstances = (JsonArray) o.get("instances");
                for (JsonValue jInstance : jInstances) {
                    LwM2mObjectInstance instance = (LwM2mObjectInstance) deserialize((JsonObject) jInstance);
                    instances.add(instance);
                }
                return new LwM2mObject(id, instances);
            }
        case "instance":
            {
                Collection<LwM2mResource> resources = new ArrayList<>();
                JsonObject jResources = (JsonObject) o.get("resources");
                for (Member jResource : jResources) {
                    LwM2mResource resource = (LwM2mResource) deserialize((JsonObject) jResource.getValue());
                    resources.add(resource);
                }
                return new LwM2mObjectInstance(id, resources);
            }
        case "singleResource":
            {
                String jType = o.getString("type", null);
                if (jType == null)
                    throw new IllegalStateException("Invalid LwM2mNode missing type attribute");
                Type type = Enum.valueOf(Type.class, jType);
                Object value = ValueSerDes.deserialize(o.get("value"), type);
                return LwM2mSingleResource.newResource(id, value, type);
            }
        case "multipleResource":
            {
                String jType = o.getString("type", null);
                if (jType == null)
                    throw new IllegalStateException("Invalid LwM2mNode missing type attribute");
                Type type = Enum.valueOf(Type.class, jType);
                Map<Integer, Object> values = new HashMap<>();
                JsonObject jValues = (JsonObject) o.get("values");
                for (Member jValue : jValues) {
                    Integer valueId = Integer.valueOf(jValue.getName());
                    Object value = ValueSerDes.deserialize(jValue.getValue(), type);
                    values.put(valueId, value);
                }
                return LwM2mMultipleResource.newResource(id, values, type);
            }
        default:
            throw new IllegalStateException("Invalid LwM2mNode missing kind attribute");
    }
}
Also used : JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) JsonArray(com.eclipsesource.json.JsonArray) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) Type(org.eclipse.leshan.core.model.ResourceModel.Type) Collection(java.util.Collection) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) JsonObject(com.eclipsesource.json.JsonObject) LwM2mObject(org.eclipse.leshan.core.node.LwM2mObject) Member(com.eclipsesource.json.JsonObject.Member) HashMap(java.util.HashMap) Map(java.util.Map)

Example 12 with JsonArray

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

the class DownlinkRequestSerDes method deserialize.

public static DownlinkRequest<?> deserialize(JsonObject o) {
    String kind = o.getString("kind", null);
    String path = o.getString("path", null);
    switch(kind) {
        case "observe":
            {
                int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
                return new ObserveRequest(ContentFormat.fromCode(format), path);
            }
        case "delete":
            return new DeleteRequest(path);
        case "discover":
            return new DiscoverRequest(path);
        case "create":
            {
                int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
                int instanceId = o.getInt("instanceId", LwM2mObjectInstance.UNDEFINED);
                Collection<LwM2mResource> resources = new ArrayList<>();
                JsonArray jResources = (JsonArray) o.get("resources");
                for (JsonValue jResource : jResources) {
                    LwM2mResource resource = (LwM2mResource) LwM2mNodeSerDes.deserialize((JsonObject) jResource);
                    resources.add(resource);
                }
                return new CreateRequest(ContentFormat.fromCode(format), path, new LwM2mObjectInstance(instanceId, resources));
            }
        case "execute":
            String parameters = o.getString("parameters", null);
            return new ExecuteRequest(path, parameters);
        case "writeAttributes":
            {
                String observeSpec = o.getString("observeSpec", null);
                return new WriteAttributesRequest(path, ObserveSpec.parse(observeSpec));
            }
        case "write":
            {
                int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
                Mode mode = o.getString("mode", "REPLACE").equals("REPLACE") ? Mode.REPLACE : Mode.UPDATE;
                LwM2mNode node = LwM2mNodeSerDes.deserialize((JsonObject) o.get("node"));
                return new WriteRequest(mode, ContentFormat.fromCode(format), path, node);
            }
        case "read":
            {
                int format = o.getInt("contentFormat", ContentFormat.TLV.getCode());
                return new ReadRequest(ContentFormat.fromCode(format), path);
            }
        default:
            throw new IllegalStateException("Invalid request missing kind attribute");
    }
}
Also used : CreateRequest(org.eclipse.leshan.core.request.CreateRequest) WriteRequest(org.eclipse.leshan.core.request.WriteRequest) Mode(org.eclipse.leshan.core.request.WriteRequest.Mode) JsonValue(com.eclipsesource.json.JsonValue) JsonObject(com.eclipsesource.json.JsonObject) LwM2mNode(org.eclipse.leshan.core.node.LwM2mNode) ObserveRequest(org.eclipse.leshan.core.request.ObserveRequest) LwM2mResource(org.eclipse.leshan.core.node.LwM2mResource) WriteAttributesRequest(org.eclipse.leshan.core.request.WriteAttributesRequest) JsonArray(com.eclipsesource.json.JsonArray) ExecuteRequest(org.eclipse.leshan.core.request.ExecuteRequest) LwM2mObjectInstance(org.eclipse.leshan.core.node.LwM2mObjectInstance) DiscoverRequest(org.eclipse.leshan.core.request.DiscoverRequest) Collection(java.util.Collection) DeleteRequest(org.eclipse.leshan.core.request.DeleteRequest) ReadRequest(org.eclipse.leshan.core.request.ReadRequest)

Example 13 with JsonArray

use of com.eclipsesource.json.JsonArray in project zencash-swing-wallet-ui by ZencashOfficial.

the class ZCashClientCaller method getWalletAllPublicAddresses.

// ./zcash-cli listreceivedbyaddress 0 true
public synchronized String[] getWalletAllPublicAddresses() throws WalletCallException, IOException, InterruptedException {
    JsonArray jsonReceivedOutputs = executeCommandAndGetJsonArray("listreceivedbyaddress", "0", "true");
    Set<String> addresses = new HashSet<>();
    for (int i = 0; i < jsonReceivedOutputs.size(); i++) {
        JsonObject outp = jsonReceivedOutputs.get(i).asObject();
        addresses.add(outp.getString("address", "ERROR!"));
    }
    return addresses.toArray(new String[0]);
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) JsonObject(com.eclipsesource.json.JsonObject) HashSet(java.util.HashSet)

Example 14 with JsonArray

use of com.eclipsesource.json.JsonArray in project zencash-swing-wallet-ui by ZencashOfficial.

the class ZCashClientCaller method getWalletPublicTransactions.

public synchronized String[][] getWalletPublicTransactions() throws WalletCallException, IOException, InterruptedException {
    String notListed = "\u26D4";
    OS_TYPE os = OSUtil.getOSType();
    if (os == OS_TYPE.WINDOWS) {
        notListed = " \u25B6";
    }
    JsonArray jsonTransactions = executeCommandAndGetJsonArray("listtransactions", wrapStringParameter(""), "300");
    String[][] strTransactions = new String[jsonTransactions.size()][];
    for (int i = 0; i < jsonTransactions.size(); i++) {
        strTransactions[i] = new String[7];
        JsonObject trans = jsonTransactions.get(i).asObject();
        // Needs to be the same as in getWalletZReceivedTransactions()
        // TODO: some day refactor to use object containers
        strTransactions[i][0] = "\u2606T (Public)";
        strTransactions[i][1] = trans.getString("category", "ERROR!");
        strTransactions[i][2] = trans.get("confirmations").toString();
        strTransactions[i][3] = trans.get("amount").toString();
        strTransactions[i][4] = trans.get("time").toString();
        strTransactions[i][5] = trans.getString("address", notListed + " (Z Address not listed by wallet!)");
        strTransactions[i][6] = trans.get("txid").toString();
    }
    return strTransactions;
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) OS_TYPE(com.vaklinov.zcashui.OSUtil.OS_TYPE) JsonObject(com.eclipsesource.json.JsonObject)

Example 15 with JsonArray

use of com.eclipsesource.json.JsonArray in project zencash-swing-wallet-ui by ZencashOfficial.

the class ZCashClientCaller method getTransactionMessagingDataForZaddress.

public synchronized JsonObject[] getTransactionMessagingDataForZaddress(String ZAddress) throws WalletCallException, IOException, InterruptedException {
    JsonArray jsonTransactions = executeCommandAndGetJsonArray("z_listreceivedbyaddress", wrapStringParameter(ZAddress), "0");
    List<JsonObject> transactions = new ArrayList<JsonObject>();
    for (int i = 0; i < jsonTransactions.size(); i++) {
        JsonObject trans = jsonTransactions.get(i).asObject();
        transactions.add(trans);
    }
    return transactions.toArray(new JsonObject[0]);
}
Also used : JsonArray(com.eclipsesource.json.JsonArray) ArrayList(java.util.ArrayList) JsonObject(com.eclipsesource.json.JsonObject)

Aggregations

JsonArray (com.eclipsesource.json.JsonArray)111 JsonObject (com.eclipsesource.json.JsonObject)96 JsonValue (com.eclipsesource.json.JsonValue)32 Test (org.junit.Test)30 URL (java.net.URL)28 ArrayList (java.util.ArrayList)27 HashMap (java.util.HashMap)11 Matchers.containsString (org.hamcrest.Matchers.containsString)6 JsonUtil.getString (com.hazelcast.util.JsonUtil.getString)5 Map (java.util.Map)5 Link (org.eclipse.leshan.Link)4 Date (java.util.Date)3 LwM2mResource (org.eclipse.leshan.core.node.LwM2mResource)3 ClientEndPointDTO (com.hazelcast.internal.management.dto.ClientEndPointDTO)2 MalformedURLException (java.net.MalformedURLException)2 DecimalFormat (java.text.DecimalFormat)2 DecimalFormatSymbols (java.text.DecimalFormatSymbols)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 LwM2mObjectInstance (org.eclipse.leshan.core.node.LwM2mObjectInstance)2