Search in sources :

Example 1 with ServoyJSONArray

use of com.servoy.j2db.util.ServoyJSONArray in project servoy-client by Servoy.

the class NGCustomJSONArrayType method toDesignValue.

@Override
public Object toDesignValue(Object value, PropertyDescription pd) {
    if (value instanceof Object[]) {
        PropertyDescription elementPD = getCustomJSONTypeDefinition();
        Object[] arr = (Object[]) value;
        JSONArray jsonArray = new ServoyJSONArray();
        for (int i = 0; i < arr.length; i++) {
            jsonArray.put(i, WebObjectImpl.convertFromJavaType(elementPD, arr[i]));
        }
        return jsonArray;
    }
    return value;
}
Also used : PropertyDescription(org.sablo.specification.PropertyDescription) ServoyJSONArray(com.servoy.j2db.util.ServoyJSONArray) JSONArray(org.json.JSONArray) ServoyJSONArray(com.servoy.j2db.util.ServoyJSONArray) JSONObject(org.json.JSONObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject)

Example 2 with ServoyJSONArray

use of com.servoy.j2db.util.ServoyJSONArray in project servoy-client by Servoy.

the class WebObjectImpl method updateJSONFromPersistMappedPropeties.

@Override
public void updateJSONFromPersistMappedPropeties() {
    // writes persist mapped properties back to json
    if (arePersistMappedPropertiesLoaded && !skipPersistMappedPropertiesUpdate) {
        JSONObject old = (JSONObject) webObject.getOwnProperty(StaticContentSpecLoader.PROPERTY_JSON.getPropertyName());
        try {
            // we have to keep the same instance if possible cause otherwise com.servoy.eclipse.designer.property.UndoablePropertySheetEntry would set child but restore completely from parent when modifying a child value in case of nested properties
            JSONObject entireModel = (old != null ? old : new ServoyJSONObject());
            Iterator<String> it = entireModel.keys();
            // remove custom properties that were removed (be sure to keep any keys that do not map to custom properties or arrays of custom properties - for example ints, string arrays and so on)
            List<String> toRemove = new ArrayList<String>();
            while (it.hasNext()) {
                String key = it.next();
                if (isPersistMappedProperty(key) && !getPersistMappedProperties().containsKey(key))
                    toRemove.add(key);
            }
            for (String key : toRemove) {
                entireModel.remove(key);
            }
            for (Map.Entry<String, Object> wo : getPersistMappedProperties().entrySet()) {
                if (wo.getValue() == null)
                    entireModel.put(wo.getKey(), JSONObject.NULL);
                else if (wo.getValue() instanceof IChildWebObject) {
                    entireModel.put(wo.getKey(), ((IChildWebObject) wo.getValue()).getFullJsonInFrmFile());
                } else {
                    ServoyJSONArray jsonArray = new ServoyJSONArray();
                    int i = 0;
                    for (IChildWebObject wo1 : (IChildWebObject[]) wo.getValue()) {
                        if (wo1 != null) {
                            wo1.setIndex(i);
                            wo1.setJsonKey(wo.getKey());
                        }
                        i++;
                        jsonArray.put(wo1 != null ? wo1.getFullJsonInFrmFile() : JSONObject.NULL);
                    }
                    entireModel.put(wo.getKey(), jsonArray);
                }
                PropertyDescription childPd = getChildPropertyDescription(wo.getKey());
                // the one defined in .spec file as default value, don't write it...)
                if (childPd.hasDefault() && Utils.areJSONEqual(childPd.getDefaultValue(), entireModel.opt(wo.getKey()), IChildWebObject.UUID_KEY)) {
                    entireModel.remove(wo.getKey());
                }
            }
            setJsonInternal(entireModel);
            ((AbstractBase) webObject).flagChanged();
            updateParentJSON();
        } catch (JSONException ex) {
            Debug.error(ex);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ServoyJSONArray(com.servoy.j2db.util.ServoyJSONArray) JSONException(org.json.JSONException) PropertyDescription(org.sablo.specification.PropertyDescription) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with ServoyJSONArray

use of com.servoy.j2db.util.ServoyJSONArray in project servoy-client by Servoy.

the class JSNGWebComponent method getJSONProperty.

@Override
public Object getJSONProperty(String propertyName) {
    WebComponent webComponent = getBaseComponent(false);
    JSONObject json = webComponent.getFlattenedJson();
    if (json == null)
        return Context.getUndefinedValue();
    Object value;
    WebObjectSpecification spec = WebComponentSpecProvider.getSpecProviderState().getWebComponentSpecification(webComponent.getTypeName());
    if (spec != null) {
        Pair<PropertyDescription, String> propAndName = getPropertyDescriptionAndName(propertyName, spec);
        if (!json.has(propAndName.getRight()) && propAndName.getLeft() != null && propAndName.getLeft().hasDefault()) {
            value = propAndName.getLeft().getDefaultValue();
        } else {
            value = json.opt(propAndName.getRight());
        }
        value = fromDesignToRhinoValue(value, propAndName.getLeft(), application, this, propertyName);
    // JSONArray and JSONObject are automatically wrapped when going to Rhino through ServoyWrapFactory, so no need to treat them specially here
    } else {
        value = json.opt(propertyName);
    }
    // so we need to make sure we always return a JSONObject.
    if (value instanceof ServoyJSONObject) {
        value = new JSONObject((ServoyJSONObject) value, ((ServoyJSONObject) value).keySet().toArray(new String[0]));
    } else if (value instanceof ServoyJSONArray) {
        ServoyJSONArray sArray = (ServoyJSONArray) value;
        JSONArray array = new JSONArray();
        for (int i = 0; i < sArray.length(); i++) {
            array.put(i, sArray.get(i));
        }
        value = sArray;
    }
    return value == null ? Context.getUndefinedValue() : ServoyJSONObject.jsonNullToNull(value);
}
Also used : PropertyDescription(org.sablo.specification.PropertyDescription) WebComponent(com.servoy.j2db.persistence.WebComponent) WebObjectSpecification(org.sablo.specification.WebObjectSpecification) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) ServoyJSONArray(com.servoy.j2db.util.ServoyJSONArray) JSONArray(org.json.JSONArray) ServoyJSONArray(com.servoy.j2db.util.ServoyJSONArray) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject)

Example 4 with ServoyJSONArray

use of com.servoy.j2db.util.ServoyJSONArray in project servoy-client by Servoy.

the class MaterialAttributesExtractor method main.

public static void main(String[] args) throws FileNotFoundException, IOException {
    FileInputStream is = new FileInputStream("test/com/servoy/j2db/server/ngclient/component/spec/material_layout.css");
    String css = IOUtils.toString(is, "UTF-8");
    is.close();
    Map<String, Set<String>> attributes = new HashMap<>();
    Pattern p = Pattern.compile("\\[(.+?)(\\=(.+?))?\\]");
    Matcher matcher = p.matcher(css);
    while (matcher.find()) {
        Set<String> list = getValues(attributes, matcher.group(1));
        if (matcher.group(3) != null) {
            list.addAll(Arrays.asList(matcher.group(3).replaceAll("\"", "").split(" ")));
        }
    }
    ServoyJSONObject obj = new ServoyJSONObject(false, true);
    for (String key : attributes.keySet()) {
        Set<String> values = attributes.get(key);
        String type = "string";
        if (!values.isEmpty()) {
            // get the first non-null value
            String v = values.iterator().next();
            if ("0".equals(v) || Utils.getAsInteger(v) != 0)
                type = "object";
        }
        ServoyJSONObject attribute = new ServoyJSONObject(false, false);
        attribute.put("type", type);
        if (type.equals("string") && values.size() > 0) {
            ServoyJSONArray arr = new ServoyJSONArray(values);
            attribute.put("values", arr);
        }
        obj.put(key, attribute);
    }
    ServoyJSONObject o = new ServoyJSONObject(false, true);
    o.put("attributes", obj);
    FileUtils.writeStringToFile(new File("test/com/servoy/j2db/server/ngclient/component/spec/attributes.json"), o.toString());
    System.out.println("Generated file attributes.json");
}
Also used : Pattern(java.util.regex.Pattern) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) Set(java.util.Set) TreeSet(java.util.TreeSet) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ServoyJSONArray(com.servoy.j2db.util.ServoyJSONArray) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

ServoyJSONArray (com.servoy.j2db.util.ServoyJSONArray)4 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)4 JSONObject (org.json.JSONObject)3 PropertyDescription (org.sablo.specification.PropertyDescription)3 HashMap (java.util.HashMap)2 JSONArray (org.json.JSONArray)2 WebComponent (com.servoy.j2db.persistence.WebComponent)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 JSONException (org.json.JSONException)1 ScriptableObject (org.mozilla.javascript.ScriptableObject)1 WebObjectSpecification (org.sablo.specification.WebObjectSpecification)1