Search in sources :

Example 1 with MarshalException

use of com.laytonsmith.core.exceptions.MarshalException in project CommandHelper by EngineHub.

the class Construct method json_decode.

/**
 * Takes a string and converts it into a Construct
 *
 * @param s
 * @param t
 * @return
 * @throws com.laytonsmith.core.exceptions.MarshalException
 */
public static Construct json_decode(String s, Target t) throws MarshalException {
    if (s == null) {
        return CNull.NULL;
    }
    if ("".equals(s.trim())) {
        throw new MarshalException();
    }
    if (s.startsWith("{")) {
        // Object
        JSONObject obj = (JSONObject) JSONValue.parse(s);
        CArray ca = CArray.GetAssociativeArray(t);
        if (obj == null) {
            // so go ahead and throw an exception
            throw new MarshalException();
        }
        for (Object key : obj.keySet()) {
            ca.set(convertJSON(key, t), convertJSON(obj.get(key), t), t);
        }
        return ca;
    } else if (s.startsWith("[")) {
        // It's an array
        JSONArray array = (JSONArray) JSONValue.parse(s);
        if (array == null) {
            throw new MarshalException();
        }
        CArray carray = new CArray(t);
        for (int i = 0; i < array.size(); i++) {
            carray.push(convertJSON(array.get(i), t), t);
        }
        return carray;
    } else {
        // It's a single value, but we're gonna wrap it in an array, then deconstruct it
        s = "[" + s + "]";
        JSONArray array = (JSONArray) JSONValue.parse(s);
        if (array == null) {
            // It's a null value
            return CNull.NULL;
        }
        Object o = array.get(0);
        return convertJSON(o, t);
    }
}
Also used : MarshalException(com.laytonsmith.core.exceptions.MarshalException) JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) JSONObject(org.json.simple.JSONObject)

Example 2 with MarshalException

use of com.laytonsmith.core.exceptions.MarshalException in project CommandHelper by EngineHub.

the class PNViewer method showData.

private void showData(final String key, String value) {
    String[] split = key.split("\\.");
    String[] namespace = new String[split.length - 1];
    String keyPart = null;
    for (int i = 0; i < split.length; i++) {
        if (i == split.length - 1) {
            keyPart = split[i];
        } else {
            namespace[i] = split[i];
        }
    }
    namespaceLabel.setText(join(namespace));
    keyLabel.setText(keyPart);
    if (value == null) {
        sourceLabel.setText("");
        valueTypeLabel.setText("(empty key)");
        valueTextArea.setText("");
    } else {
        sourceLabel.setText("(resolving)");
        new Thread(new Runnable() {

            @Override
            public void run() {
                final String source = network.getKeySource(key.split("\\.")).toString();
                SwingUtilities.invokeLater(new Runnable() {

                    @Override
                    public void run() {
                        sourceLabel.setText(source);
                    }
                });
            }
        }).start();
        Construct c = CNull.NULL;
        try {
            c = Construct.json_decode(value, Target.UNKNOWN);
        } catch (MarshalException ex) {
            Logger.getLogger(PNViewer.class.getName()).log(Level.SEVERE, null, ex);
        }
        valueTypeLabel.setText(new DataHandling.typeof().exec(Target.UNKNOWN, null, c).val());
        valueTextArea.setText(c.val());
    }
}
Also used : MarshalException(com.laytonsmith.core.exceptions.MarshalException) Construct(com.laytonsmith.core.constructs.Construct) DataHandling(com.laytonsmith.core.functions.DataHandling)

Aggregations

MarshalException (com.laytonsmith.core.exceptions.MarshalException)2 Construct (com.laytonsmith.core.constructs.Construct)1 DataHandling (com.laytonsmith.core.functions.DataHandling)1 JSONArray (org.json.simple.JSONArray)1 JSONObject (org.json.simple.JSONObject)1