Example usage for javax.json JsonValue getValueType

List of usage examples for javax.json JsonValue getValueType

Introduction

In this page you can find the example usage for javax.json JsonValue getValueType.

Prototype

ValueType getValueType();

Source Link

Document

Returns the value type of this JSON value.

Usage

From source file:org.hyperledger.fabric.sdk.NetworkConfig.java

private static String getJsonValueAsString(JsonValue value) {
    return (value != null && value.getValueType() == ValueType.STRING) ? ((JsonString) value).getString()
            : null;//from   w ww  .j av  a 2s .  co  m
}

From source file:org.hyperledger.fabric.sdk.NetworkConfig.java

private static JsonObject getJsonObject(JsonObject object, String propName) {
    JsonObject obj = null;/*  ww w .j  av a2 s  .  c  om*/
    JsonValue val = object.get(propName);
    if (val != null && val.getValueType() == ValueType.OBJECT) {
        obj = val.asJsonObject();
    }
    return obj;
}

From source file:org.hyperledger.fabric.sdk.NetworkConfig.java

private static Boolean getJsonValueAsBoolean(JsonValue value) {
    if (value != null) {
        if (value.getValueType() == ValueType.TRUE) {
            return true;
        } else if (value.getValueType() == ValueType.FALSE) {
            return false;
        }/*  ww w  . j a  va  2s .com*/
    }
    return null;
}

From source file:org.apache.johnzon.maven.plugin.ExampleToModelMojo.java

private void handleArray(final Writer writer, final String prefix, final Map<String, JsonObject> nestedTypes,
        final JsonValue value, final String jsonField, final String fieldName, final int arrayLevel,
        final Collection<String> imports) throws IOException {
    final JsonArray array = JsonArray.class.cast(value);
    if (array.size() > 0) { // keep it simple for now - 1 level, we can have an awesome recursive algo later if needed
        final JsonValue jsonValue = array.get(0);
        switch (jsonValue.getValueType()) {
        case OBJECT:
            final String javaName = toJavaName(fieldName);
            nestedTypes.put(javaName, JsonObject.class.cast(jsonValue));
            fieldGetSetMethods(writer, jsonField, fieldName, javaName, prefix, arrayLevel, imports);
            break;
        case TRUE:
        case FALSE:
            fieldGetSetMethods(writer, jsonField, fieldName, "Boolean", prefix, arrayLevel, imports);
            break;
        case NUMBER:
            fieldGetSetMethods(writer, jsonField, fieldName, "Double", prefix, arrayLevel, imports);
            break;
        case STRING:
            fieldGetSetMethods(writer, jsonField, fieldName, "String", prefix, arrayLevel, imports);
            break;
        case ARRAY:
            handleArray(writer, prefix, nestedTypes, jsonValue, jsonField, fieldName, arrayLevel + 1, imports);
            break;
        case NULL:
        default:/*from  w  w w. j  av a 2 s.  c  om*/
            throw new UnsupportedOperationException("Unsupported " + value + ".");
        }
    } else {
        getLog().warn("Empty arrays are ignored");
    }
}

From source file:org.hyperledger.fabric.sdk.NetworkConfig.java

private static List<JsonObject> getJsonValueAsList(JsonValue value) {
    if (value != null) {
        if (value.getValueType() == ValueType.ARRAY) {
            return value.asJsonArray().getValuesAs(JsonObject.class);

        } else if (value.getValueType() == ValueType.OBJECT) {
            List<JsonObject> ret = new ArrayList<>();
            ret.add(value.asJsonObject());

            return ret;
        }//from   w  ww .jav a2 s  . c om
    }
    return null;
}

From source file:org.hyperledger.fabric.sdk.ChaincodeCollectionConfiguration.java

Collection.CollectionConfigPackage parse(JsonArray jsonConfig)
        throws ChaincodeCollectionConfigurationException {

    Collection.CollectionConfigPackage.Builder colcofbuilder = Collection.CollectionConfigPackage.newBuilder();
    for (int i = jsonConfig.size() - 1; i > -1; --i) {

        Collection.StaticCollectionConfig.Builder ssc = Collection.StaticCollectionConfig.newBuilder();

        JsonValue j = jsonConfig.get(i);
        if (j.getValueType() != JsonValue.ValueType.OBJECT) {
            throw new ChaincodeCollectionConfigurationException(format(
                    "Expected StaticCollectionConfig to be Object type but got: %s", j.getValueType().name()));
        }/*ww w  . j a  v a2 s  . c  o  m*/

        JsonObject jsonObject = j.asJsonObject();
        JsonObject scf = getJsonObject(jsonObject, "StaticCollectionConfig"); // oneof .. may have different values in the future
        ssc.setName(getJsonString(scf, "name")).setBlockToLive(getJsonLong(scf, "blockToLive"))
                .setMaximumPeerCount(getJsonInt(scf, "maximumPeerCount"))
                .setMemberOrgsPolicy(Collection.CollectionPolicyConfig.newBuilder()
                        .setSignaturePolicy(parseSignaturePolicyEnvelope(scf)).build())
                .setRequiredPeerCount(getJsonInt(scf, "requiredPeerCount"));

        colcofbuilder
                .addConfig(Collection.CollectionConfig.newBuilder().setStaticCollectionConfig(ssc).build());

    }
    return colcofbuilder.build();

}

From source file:org.hyperledger.fabric.sdk.ChaincodeCollectionConfiguration.java

private IndexedHashMap<String, MSPPrincipal> parseIdentities(JsonArray identities)
        throws ChaincodeCollectionConfigurationException {
    IndexedHashMap<String, MSPPrincipal> ret = new IndexedHashMap<>();

    for (JsonValue jsonValue : identities) {
        if (jsonValue.getValueType() != JsonValue.ValueType.OBJECT) {
            throw new ChaincodeCollectionConfigurationException(
                    format("Expected in identies user to be Object type but got: %s",
                            jsonValue.getValueType().name()));
        }/*from  ww  w .ja  v a2  s.  c o  m*/
        JsonObject user = jsonValue.asJsonObject();
        if (user.entrySet().size() != 1) {
            throw new ChaincodeCollectionConfigurationException(
                    "Only expected on property for user entry in identies.");
        }
        Map.Entry<String, JsonValue> next = user.entrySet().iterator().next();
        String name = next.getKey();
        jsonValue = next.getValue();
        if (jsonValue.getValueType() != JsonValue.ValueType.OBJECT) {
            throw new ChaincodeCollectionConfigurationException(
                    format("Expected in identies role to be Object type but got: %s",
                            jsonValue.getValueType().name()));
        }
        JsonObject role = jsonValue.asJsonObject();
        JsonObject roleObj = getJsonObject(role, "role");
        String roleName = getJsonString(roleObj, "name");
        String mspId = getJsonString(roleObj, "mspId");

        MSPRole.MSPRoleType mspRoleType;

        switch (roleName.intern()) {
        case "member":
            mspRoleType = MSPRole.MSPRoleType.MEMBER;
            break;
        case "admin":
            mspRoleType = MSPRole.MSPRoleType.ADMIN;
            break;
        case "client":
            mspRoleType = MSPRole.MSPRoleType.CLIENT;
            break;
        case "peer":
            mspRoleType = MSPRole.MSPRoleType.PEER;
            break;
        default:
            throw new ChaincodeCollectionConfigurationException(format(
                    "In identities with key %s name expected member, admin, client, or peer in role got %s ",
                    name, roleName));
        }

        MSPRole mspRole = MSPRole.newBuilder().setRole(mspRoleType).setMspIdentifier(mspId).build();

        MSPPrincipal principal = MSPPrincipal.newBuilder()
                .setPrincipalClassification(MSPPrincipal.Classification.ROLE)
                .setPrincipal(mspRole.toByteString()).build();

        ret.put(name, principal);

    }

    return ret;
}

From source file:org.hyperledger.fabric.sdk.ChaincodeCollectionConfiguration.java

private SignaturePolicy parsePolicy(IndexedHashMap<String, MSPPrincipal> identities, JsonObject policy)
        throws ChaincodeCollectionConfigurationException {

    if (policy.size() != 1) {
        throw new ChaincodeCollectionConfigurationException(
                format("Expected policy size of 1 but got %d", policy.size()));
    }/*  w  w w  . j  a  v  a  2 s.co m*/
    final String key = policy.entrySet().iterator().next().getKey();

    if ("signed-by".equals(key)) {
        final String vo = getJsonString(policy, key);

        MSPPrincipal mspPrincipal = identities.get(vo);
        if (null == mspPrincipal) {
            throw new ChaincodeCollectionConfigurationException(
                    format("No identity found by name %s in signed-by.", vo));
        }

        return SignaturePolicy.newBuilder().setSignedBy(identities.getKeysIndex(vo)).build();

    } else {

        Matcher match = noofPattern.matcher(key);
        final JsonArray vo = getJsonArray(policy, key);

        if (match.matches() && match.groupCount() == 1) {

            String matchStingNo = match.group(1).trim();
            int matchNo = Integer.parseInt(matchStingNo);

            if (vo.size() < matchNo) {

                throw new ChaincodeCollectionConfigurationException(
                        format("%s expected to have at least %d items to match but only found %d.", key,
                                matchNo, vo.size()));
            }

            SignaturePolicy.NOutOf.Builder spBuilder = SignaturePolicy.NOutOf.newBuilder().setN(matchNo);

            for (int i = vo.size() - 1; i >= 0; --i) {
                JsonValue jsonValue = vo.get(i);
                if (jsonValue.getValueType() != JsonValue.ValueType.OBJECT) {
                    throw new ChaincodeCollectionConfigurationException(
                            format("Expected object type in Nof but got %s", jsonValue.getValueType().name()));
                }

                SignaturePolicy sp = parsePolicy(identities, jsonValue.asJsonObject());
                spBuilder.addRules(sp);

            }

            return SignaturePolicy.newBuilder().setNOutOf(spBuilder.build()).build();

        } else {

            throw new ChaincodeCollectionConfigurationException(format("Unsupported policy type %s", key));
        }
    }

}

From source file:org.btc4j.daemon.BtcJsonRpcHttpClient.java

public String jsonString(JsonValue value) throws BtcException {
    return ((value != null) && (value.getValueType() == JsonValue.ValueType.STRING)
            && (value instanceof JsonString)) ? ((JsonString) value).getString() : "";
}

From source file:org.btc4j.daemon.BtcJsonRpcHttpClient.java

public long jsonLong(JsonValue value) throws BtcException {
    if ((value == null) || (value.getValueType() == JsonValue.ValueType.NULL)) {
        return 0;
    }//from   www .j  a  v  a 2  s.  c  o m
    if ((value.getValueType() == JsonValue.ValueType.NUMBER) && (value instanceof JsonNumber)) {
        return ((JsonNumber) value).longValue();
    }
    throw new BtcException(BtcException.BTC4J_ERROR_CODE,
            BtcException.BTC4J_ERROR_MESSAGE + ": " + BTC4J_DAEMON_DATA_INVALID_TYPE + value.getValueType());
}