Search in sources :

Example 26 with JSONObject

use of org.talend.utils.json.JSONObject in project tbd-studio-se by Talend.

the class DefaultConfigurationManagerTest method testGetValue_NotExisted.

@Test
public void testGetValue_NotExisted() throws JSONException {
    JSONObject json = new JSONObject();
    json.put("key1", "123");
    assertNull(DefaultConfigurationManager.getValue(json, "abc"));
}
Also used : JSONObject(org.talend.utils.json.JSONObject) Test(org.junit.Test)

Example 27 with JSONObject

use of org.talend.utils.json.JSONObject in project tbd-studio-se by Talend.

the class DefaultConfigurationManagerTest method testGetValue_Whole.

@Test
public void testGetValue_Whole() throws JSONException {
    JSONObject json = new JSONObject();
    json.put("key1", "123");
    String value = DefaultConfigurationManager.getValue(json, "");
    assertNotNull(value);
    JSONObject getJson = null;
    try {
        getJson = new JSONObject(value);
    } catch (JSONException e) {
    // 
    }
    assertNotNull("Can't get the whole JSON", getJson);
    assertTrue(getJson.has("key1"));
    assertEquals("123", getJson.getString("key1"));
}
Also used : JSONObject(org.talend.utils.json.JSONObject) JSONException(org.talend.utils.json.JSONException) Test(org.junit.Test)

Example 28 with JSONObject

use of org.talend.utils.json.JSONObject in project tbd-studio-se by Talend.

the class AbstractTest4DefaultConfiguration method testDefaultConfiguration_Existed.

// @Test
public void testDefaultConfiguration_Existed() throws JSONException {
    HadoopComponent hadoopComponent = getHadoopComponent();
    final String distribution = hadoopComponent.getDistribution();
    String version = hadoopComponent.getVersion();
    if (version == null) {
        version = "";
    }
    String notFoundMessages = MessageFormat.format("The default configuration of the version \"{0}\" for \"{1}\" is not found", version, distribution);
    // get all
    String wholeConfig = hadoopComponent.getDefaultConfig(distribution, "");
    assertNotNull(notFoundMessages, wholeConfig);
    JSONObject wholeJson = new JSONObject(wholeConfig);
    // should existed one key at least.
    assertTrue(notFoundMessages, wholeJson.length() > 0);
}
Also used : JSONObject(org.talend.utils.json.JSONObject) HadoopComponent(org.talend.hadoop.distribution.component.HadoopComponent)

Example 29 with JSONObject

use of org.talend.utils.json.JSONObject in project tdi-studio-se by Talend.

the class JsonTableHandler method getDataRowList.

private List<List<Object>> getDataRowList(JSONObject rootObj, AtomicInteger columnsCount) throws JSONException {
    List<List<Object>> rows = new ArrayList<>();
    AtomicInteger count = columnsCount;
    if (count == null) {
        count = new AtomicInteger();
    }
    JSONArray jsonArr = rootObj.getJSONArray(DATA_KEY);
    for (int i = 0; i < jsonArr.length(); i++) {
        List<Object> row = new ArrayList<>();
        JSONArray rowArray = jsonArr.getJSONArray(i);
        for (int j = 0; j < rowArray.length(); j++) {
            row.add(rowArray.get(j));
        }
        if (row.size() > count.get()) {
            count.set(row.size());
        }
        rows.add(row);
    }
    return rows;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) JSONArray(org.talend.utils.json.JSONArray) List(java.util.List) ArrayList(java.util.ArrayList) JSONObject(org.talend.utils.json.JSONObject)

Example 30 with JSONObject

use of org.talend.utils.json.JSONObject in project tdi-studio-se by Talend.

the class JsonTableHandler method parse.

public JsonTableVO parse(String jsonStr) {
    JsonTableVO vo = null;
    try {
        JSONObject rootObj = new JSONObject(jsonStr);
        List<String> columnTitles = getColumnNames(rootObj);
        AtomicInteger maxColumnsCount = new AtomicInteger();
        List<List<Object>> dataRowList = getDataRowList(rootObj, maxColumnsCount);
        if (columnTitles.size() == 0) {
            columnTitles = getColumnTitles(maxColumnsCount.get());
        }
        vo = buildVO(columnTitles, dataRowList);
    } catch (Exception e) {
        //$NON-NLS-1$
        log.warn(Messages.getString("JsonTableHandler.parse.warnMsg", jsonStr), e);
    }
    return vo;
}
Also used : JSONObject(org.talend.utils.json.JSONObject) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) ArrayList(java.util.ArrayList) JSONException(org.talend.utils.json.JSONException)

Aggregations

JSONObject (org.talend.utils.json.JSONObject)36 JSONException (org.talend.utils.json.JSONException)21 JSONArray (org.talend.utils.json.JSONArray)19 Test (org.junit.Test)9 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)4 NoSQLConnection (org.talend.repository.model.nosql.NoSQLConnection)4 Iterator (java.util.Iterator)3 List (java.util.List)3 Map (java.util.Map)3 PersistenceException (org.talend.commons.exception.PersistenceException)3 EHadoopParamName (org.talend.metadata.managment.ui.utils.ExtendedNodeConnectionContextUtils.EHadoopParamName)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 SystemException (org.talend.commons.exception.SystemException)2 Context (org.talend.core.context.Context)2 RepositoryContext (org.talend.core.context.RepositoryContext)2 ConnectionBean (org.talend.core.model.general.ConnectionBean)2 Project (org.talend.core.model.general.Project)2 PreferenceManipulator (org.talend.core.prefs.PreferenceManipulator)2