Search in sources :

Example 6 with LinkedTreeMap

use of com.google.gson.internal.LinkedTreeMap in project components by Talend.

the class MarketoBaseRESTClientTest method testParseRecords.

@Test
public void testParseRecords() throws Exception {
    Schema schema = // 
    SchemaBuilder.builder().record("test").fields().name("field1").type().nullable().stringType().noDefault().name("fields").type().nullable().stringType().noDefault().name("field2").type().nullable().intType().noDefault().name("field3").type().nullable().longType().noDefault().name("field4").type().nullable().booleanType().noDefault().name("field5").type().nullable().doubleType().noDefault().name("field6").type().nullable().floatType().noDefault().name(// 
    "field7").prop(SchemaConstants.TALEND_COLUMN_PATTERN, // 
    DATETIME_PATTERN_REST).prop(SchemaConstants.JAVA_CLASS_FLAG, // 
    Date.class.getCanonicalName()).type(AvroUtils._logicalTimestamp()).noDefault().name(// 
    "field8").prop(SchemaConstants.TALEND_COLUMN_PATTERN, // 
    DATETIME_PATTERN_REST).prop(SchemaConstants.JAVA_CLASS_FLAG, // 
    Date.class.getCanonicalName()).type(AvroUtils._logicalTimestamp()).noDefault().name("field9").type().nullType().noDefault().endRecord();
    // 
    List<LinkedTreeMap> values = new ArrayList<>();
    LinkedTreeMap map = new LinkedTreeMap();
    map.put("field1", "value1");
    map.put("fields", "{value1: 1223}");
    map.put("field2", 12345);
    map.put("field3", 1234567890L);
    map.put("field4", true);
    map.put("field5", 123456.5);
    map.put("field6", 123456.5);
    map.put("field7", new Date());
    map.put("field8", new SimpleDateFormat(DATETIME_PATTERN_REST).format(new Date().getTime()));
    map.put("field9", "nullType");
    values.add(map);
    // 
    assertEquals(Collections.emptyList(), client.parseRecords(values, null));
    assertEquals(Collections.emptyList(), client.parseRecords(null, schema));
    List<IndexedRecord> records = client.parseRecords(values, schema);
    assertNotNull(records);
    IndexedRecord record = records.get(0);
    assertNotNull(record);
    assertEquals("value1", record.get(0));
    assertEquals("\"{value1: 1223}\"", record.get(1));
    assertEquals(12345, record.get(2));
    assertEquals(1234567890L, record.get(3));
    assertEquals(true, record.get(4));
    assertEquals(123456.5, record.get(5));
    assertEquals((float) 123456.5, record.get(6));
    assertEquals(null, record.get(7));
    assertTrue(record.get(8) instanceof Long);
    assertEquals("nullType", record.get(9));
}
Also used : LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Test(org.junit.Test)

Example 7 with LinkedTreeMap

use of com.google.gson.internal.LinkedTreeMap in project components by Talend.

the class MarketoBaseRESTClient method parseRecords.

public List<IndexedRecord> parseRecords(List<LinkedTreeMap> values, Schema schema) {
    List<IndexedRecord> records = new ArrayList<>();
    if (values == null || schema == null) {
        return records;
    }
    for (LinkedTreeMap r : values) {
        IndexedRecord record = new GenericData.Record(schema);
        for (Field f : schema.getFields()) {
            Object o = r.get(f.name());
            record.put(f.pos(), getValueType(f, o));
        }
        records.add(record);
    }
    return records;
}
Also used : Field(org.apache.avro.Schema.Field) IndexedRecord(org.apache.avro.generic.IndexedRecord) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) ArrayList(java.util.ArrayList) IndexedRecord(org.apache.avro.generic.IndexedRecord) JsonObject(com.google.gson.JsonObject)

Example 8 with LinkedTreeMap

use of com.google.gson.internal.LinkedTreeMap in project components by Talend.

the class MarketoBaseRESTClient method executeGetRequest.

public MarketoRecordResult executeGetRequest(Schema schema) throws MarketoException {
    try {
        URL url = new URL(current_uri.toString());
        HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection();
        urlConn.setRequestMethod("GET");
        urlConn.setDoOutput(true);
        urlConn.setRequestProperty(REQUEST_PROPERTY_ACCEPT, REQUEST_VALUE_TEXT_JSON);
        int responseCode = urlConn.getResponseCode();
        if (responseCode == 200) {
            InputStream inStream = urlConn.getInputStream();
            Reader reader = new InputStreamReader(inStream);
            Gson gson = new Gson();
            MarketoRecordResult mkr = new MarketoRecordResult();
            LinkedTreeMap ltm = (LinkedTreeMap) gson.fromJson(reader, Object.class);
            mkr.setRequestId(REST + "::" + ltm.get("requestId"));
            mkr.setSuccess(Boolean.parseBoolean(ltm.get("success").toString()));
            mkr.setStreamPosition((String) ltm.get(FIELD_NEXT_PAGE_TOKEN));
            if (!mkr.isSuccess() && ltm.get(FIELD_ERRORS) != null) {
                List<LinkedTreeMap> errors = (List<LinkedTreeMap>) ltm.get(FIELD_ERRORS);
                for (LinkedTreeMap err : errors) {
                    MarketoError error = new MarketoError(REST, (String) err.get("code"), (String) err.get("message"));
                    mkr.setErrors(Arrays.asList(error));
                }
            }
            if (mkr.isSuccess()) {
                List<LinkedTreeMap> tmp = (List<LinkedTreeMap>) ltm.get("result");
                if (tmp != null) {
                    mkr.setRecordCount(tmp.size());
                    mkr.setRecords(parseRecords(tmp, schema));
                }
                if (mkr.getStreamPosition() != null) {
                    mkr.setRemainCount(mkr.getRecordCount());
                }
            }
            return mkr;
        } else {
            LOG.error("GET request failed: {}", responseCode);
            throw new MarketoException(REST, responseCode, "Request failed! Please check your request setting!");
        }
    } catch (IOException e) {
        LOG.error("GET request failed: {}", e.getMessage());
        throw new MarketoException(REST, e.getMessage());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) InputStream(java.io.InputStream) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Gson(com.google.gson.Gson) IOException(java.io.IOException) URL(java.net.URL) MarketoError(org.talend.components.marketo.runtime.client.type.MarketoError) JsonObject(com.google.gson.JsonObject) ArrayList(java.util.ArrayList) List(java.util.List) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) MarketoRecordResult(org.talend.components.marketo.runtime.client.type.MarketoRecordResult)

Example 9 with LinkedTreeMap

use of com.google.gson.internal.LinkedTreeMap in project ballerina by ballerina-lang.

the class CommandExecutor method executeAddAllDocumentation.

/**
 * Generate workspace edit for generating doc comments for all top level nodes and resources.
 * @param context   Workspace Service Context
 */
private static void executeAddAllDocumentation(WorkspaceServiceContext context) {
    String documentUri = "";
    VersionedTextDocumentIdentifier textDocumentIdentifier = new VersionedTextDocumentIdentifier();
    for (Object arg : context.get(ExecuteCommandKeys.COMMAND_ARGUMENTS_KEY)) {
        if (((LinkedTreeMap) arg).get(ARG_KEY).equals(CommandConstants.ARG_KEY_DOC_URI)) {
            documentUri = (String) ((LinkedTreeMap) arg).get(ARG_VALUE);
            textDocumentIdentifier.setUri(documentUri);
            context.put(DocumentServiceKeys.FILE_URI_KEY, documentUri);
        }
    }
    BLangPackage bLangPackage = TextDocumentServiceUtil.getBLangPackage(context, context.get(ExecuteCommandKeys.DOCUMENT_MANAGER_KEY), false, LSCustomErrorStrategy.class, false).get(0);
    String fileContent = context.get(ExecuteCommandKeys.DOCUMENT_MANAGER_KEY).getFileContent(Paths.get(URI.create(documentUri)));
    String[] contentComponents = fileContent.split(System.lineSeparator());
    List<TextEdit> textEdits = new ArrayList<>();
    bLangPackage.topLevelNodes.forEach(topLevelNode -> {
        CommandUtil.DocAttachmentInfo docAttachmentInfo = getDocumentEditForNode(topLevelNode);
        if (docAttachmentInfo != null) {
            textEdits.add(getTextEdit(docAttachmentInfo, contentComponents));
        }
        if (topLevelNode instanceof BLangService) {
            ((BLangService) topLevelNode).getResources().forEach(bLangResource -> {
                CommandUtil.DocAttachmentInfo resourceInfo = getDocumentEditForNode(bLangResource);
                if (resourceInfo != null) {
                    textEdits.add(getTextEdit(resourceInfo, contentComponents));
                }
            });
        }
    });
    TextDocumentEdit textDocumentEdit = new TextDocumentEdit(textDocumentIdentifier, textEdits);
    applyWorkspaceEdit(Collections.singletonList(textDocumentEdit), context.get(ExecuteCommandKeys.LANGUAGE_SERVER_KEY).getClient());
}
Also used : LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) ArrayList(java.util.ArrayList) TextDocumentEdit(org.eclipse.lsp4j.TextDocumentEdit) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) BLangPackage(org.wso2.ballerinalang.compiler.tree.BLangPackage) TextEdit(org.eclipse.lsp4j.TextEdit) LSCustomErrorStrategy(org.ballerinalang.langserver.common.LSCustomErrorStrategy)

Example 10 with LinkedTreeMap

use of com.google.gson.internal.LinkedTreeMap in project lite-apps by chimbori.

the class LiteAppsValidator method validateSettings.

private void validateSettings(String tag, File manifest) {
    Gson gson = GsonInstance.getPrettyPrinter();
    LinkedTreeMap<String, Object> json = null;
    try {
        // noinspection unchecked
        json = (LinkedTreeMap<String, Object>) gson.fromJson(new FileReader(manifest), Object.class);
    } catch (FileNotFoundException e) {
        fail(e.getMessage());
    }
    // noinspection unchecked
    LinkedTreeMap settings = (LinkedTreeMap<String, Object>) json.get("hermit_settings");
    if (settings != null) {
        for (Object key : settings.keySet()) {
            assertTrue(String.format("Unexpected setting found: [%s] in [%s]", key, tag), SETTINGS_SET.contains(key));
        }
    }
}
Also used : LinkedTreeMap(com.google.gson.internal.LinkedTreeMap) FileNotFoundException(java.io.FileNotFoundException) Gson(com.google.gson.Gson) FileReader(java.io.FileReader)

Aggregations

LinkedTreeMap (com.google.gson.internal.LinkedTreeMap)30 Gson (com.google.gson.Gson)15 ArrayList (java.util.ArrayList)10 JsonObject (com.google.gson.JsonObject)6 HashMap (java.util.HashMap)6 InputStreamReader (java.io.InputStreamReader)5 URL (java.net.URL)5 List (java.util.List)5 IOException (java.io.IOException)4 InputStream (java.io.InputStream)4 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)3 LSCustomErrorStrategy (org.ballerinalang.langserver.common.LSCustomErrorStrategy)3 VersionedTextDocumentIdentifier (org.eclipse.lsp4j.VersionedTextDocumentIdentifier)3 MarketoException (org.talend.components.marketo.runtime.client.type.MarketoException)3 MarketoRecordResult (org.talend.components.marketo.runtime.client.type.MarketoRecordResult)3 BLangPackage (org.wso2.ballerinalang.compiler.tree.BLangPackage)3 NonNull (android.support.annotation.NonNull)2 Tuple2 (bisq.common.util.Tuple2)2 JsonElement (com.google.gson.JsonElement)2 StatusCallback (com.instructure.canvasapi2.StatusCallback)2