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;
}
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);
}
}
}
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);
}
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");
}
Aggregations