Search in sources :

Example 36 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project keycloak by keycloak.

the class ImportUtils method getRealmsFromStream.

public static Map<String, RealmRepresentation> getRealmsFromStream(ObjectMapper mapper, InputStream is) throws IOException {
    Map<String, RealmRepresentation> result = new HashMap<String, RealmRepresentation>();
    JsonFactory factory = mapper.getFactory();
    JsonParser parser = factory.createParser(is);
    try {
        parser.nextToken();
        if (parser.getCurrentToken() == JsonToken.START_ARRAY) {
            // Case with more realms in stream
            parser.nextToken();
            List<RealmRepresentation> realmReps = new ArrayList<RealmRepresentation>();
            while (parser.getCurrentToken() == JsonToken.START_OBJECT) {
                RealmRepresentation realmRep = parser.readValueAs(RealmRepresentation.class);
                parser.nextToken();
                // Ensure that master realm is imported first
                if (Config.getAdminRealm().equals(realmRep.getRealm())) {
                    realmReps.add(0, realmRep);
                } else {
                    realmReps.add(realmRep);
                }
            }
            for (RealmRepresentation realmRep : realmReps) {
                result.put(realmRep.getRealm(), realmRep);
            }
        } else if (parser.getCurrentToken() == JsonToken.START_OBJECT) {
            // Case with single realm in stream
            RealmRepresentation realmRep = parser.readValueAs(RealmRepresentation.class);
            result.put(realmRep.getRealm(), realmRep);
        }
    } finally {
        parser.close();
    }
    return result;
}
Also used : HashMap(java.util.HashMap) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) JsonFactory(com.fasterxml.jackson.core.JsonFactory) ArrayList(java.util.ArrayList) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 37 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project keycloak by keycloak.

the class ExportUtils method exportFederatedUsersToStream.

public static void exportFederatedUsersToStream(KeycloakSession session, RealmModel realm, List<String> usersToExport, ObjectMapper mapper, OutputStream os, ExportOptions options) throws IOException {
    JsonFactory factory = mapper.getFactory();
    JsonGenerator generator = factory.createGenerator(os, JsonEncoding.UTF8);
    try {
        if (mapper.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
            generator.useDefaultPrettyPrinter();
        }
        generator.writeStartObject();
        generator.writeStringField("realm", realm.getName());
        // generator.writeStringField("strategy", strategy.toString());
        generator.writeFieldName("federatedUsers");
        generator.writeStartArray();
        for (String userId : usersToExport) {
            UserRepresentation userRep = ExportUtils.exportFederatedUser(session, realm, userId, options);
            generator.writeObject(userRep);
        }
        generator.writeEndArray();
        generator.writeEndObject();
    } finally {
        generator.close();
    }
}
Also used : JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) UserRepresentation(org.keycloak.representations.idm.UserRepresentation)

Example 38 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project BIMserver by opensourceBIM.

the class StreamingModel method processDownload.

private void processDownload(Long topicId) throws UserException, ServerException, PublicInterfaceNotFoundException, IfcModelInterfaceException, IOException {
    InputStream inputStream = bimServerClient.getDownloadData(topicId);
    if (inputStream == null) {
        throw new IfcModelInterfaceException("No InputStream to read from for topicId " + topicId);
    }
    InputStream downloadData = new org.bimserver.utils.CountingInputStream(inputStream) {
    };
    try {
        JsonFactory jsonFactory = new JsonFactory();
        JsonParser jp = jsonFactory.createParser(downloadData);
    // TODO implement
    } catch (Exception e) {
        throw new IfcModelInterfaceException(e);
    } finally {
        if (downloadData != null) {
            downloadData.close();
        }
    }
}
Also used : IfcModelInterfaceException(org.bimserver.emf.IfcModelInterfaceException) InputStream(java.io.InputStream) JsonFactory(com.fasterxml.jackson.core.JsonFactory) PublicInterfaceNotFoundException(org.bimserver.shared.exceptions.PublicInterfaceNotFoundException) IOException(java.io.IOException) DeserializeException(org.bimserver.plugins.deserializers.DeserializeException) IfcModelInterfaceException(org.bimserver.emf.IfcModelInterfaceException) UserException(org.bimserver.shared.exceptions.UserException) ServerException(org.bimserver.shared.exceptions.ServerException) JsonParser(com.fasterxml.jackson.core.JsonParser)

Example 39 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project components by Talend.

the class FixedDatasetRuntime method getValues.

public List<IndexedRecord> getValues(int limit) {
    List<IndexedRecord> values = new ArrayList<>();
    switch(properties.format.getValue()) {
        case CSV:
            try {
                CsvRecordToIndexedRecordConverter converter = new CsvRecordToIndexedRecordConverter(getSchema());
                for (CSVRecord r : // 
                CSVFormat.RFC4180.withDelimiter(// 
                properties.getFieldDelimiter().charAt(0)).withRecordSeparator(properties.getRecordDelimiter()).parse(new StringReader(properties.values.getValue()))) values.add(converter.convertToAvro(r));
            } catch (IOException e) {
                throw LocalIOErrorCode.createCannotParseSchema(e, properties.values.getValue());
            }
            break;
        case JSON:
            ObjectMapper mapper = new ObjectMapper();
            JsonSchemaInferrer jsonSchemaInferrer = new JsonSchemaInferrer(mapper);
            JsonGenericRecordConverter converter = null;
            JsonFactory jsonFactory = new JsonFactory();
            try (StringReader r = new StringReader(properties.values.getValue())) {
                Iterator<JsonNode> value = mapper.readValues(jsonFactory.createParser(r), JsonNode.class);
                int count = 0;
                while (value.hasNext() && count++ < limit) {
                    String json = value.next().toString();
                    if (converter == null) {
                        Schema jsonSchema = jsonSchemaInferrer.inferSchema(json);
                        converter = new JsonGenericRecordConverter(jsonSchema);
                    }
                    values.add(converter.convertToAvro(json));
                }
            } catch (IOException e) {
                throw LocalIOErrorCode.createCannotParseJson(e, properties.schema.getValue(), properties.values.getValue());
            }
            break;
        case AVRO:
            Schema schema = getSchema();
            if (isRandom()) {
                GeneratorFunction<IndexedRecord> gf = (GeneratorFunction<IndexedRecord>) GeneratorFunctions.of(getSchema());
                GeneratorFunction.GeneratorContext ctx = GeneratorFunction.GeneratorContext.of(0, 0L);
                for (int i = 0; i < limit; i++) {
                    ctx.setRowId(i);
                    values.add(gf.apply(ctx));
                }
            } else {
                try (ByteArrayInputStream bais = new ByteArrayInputStream(properties.values.getValue().trim().getBytes())) {
                    JsonDecoder decoder = DecoderFactory.get().jsonDecoder(schema, bais);
                    DatumReader<IndexedRecord> reader = new GenericDatumReader<>(schema);
                    int count = 0;
                    while (count++ < limit) {
                        values.add(reader.read(null, decoder));
                    }
                } catch (EOFException e) {
                // Indicates the end of the values.
                } catch (IOException e) {
                    throw LocalIOErrorCode.createCannotParseAvroJson(e, properties.schema.getValue(), properties.values.getValue());
                }
            }
            break;
    }
    return values;
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) JsonDecoder(org.apache.avro.io.JsonDecoder) ByteArrayInputStream(java.io.ByteArrayInputStream) StringReader(java.io.StringReader) EOFException(java.io.EOFException) CSVRecord(org.apache.commons.csv.CSVRecord) JsonGenericRecordConverter(org.talend.daikon.avro.converter.JsonGenericRecordConverter) GeneratorFunction(org.talend.components.adapter.beam.io.rowgenerator.GeneratorFunction) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonSchemaInferrer(org.talend.daikon.avro.inferrer.JsonSchemaInferrer)

Example 40 with JsonFactory

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory in project atlasdb by palantir.

the class AtlasConsoleServiceTest method fromJson.

private <T> Expectations fromJson(final T input, final Class<T> clazz) throws IOException {
    final JsonFactory factory = context.mock(JsonFactory.class);
    final JsonParser parser = context.mock(JsonParser.class);
    return new Expectations() {

        {
            oneOf(mapper).getFactory();
            will(returnValue(factory));
            oneOf(factory).createParser(QUERY);
            will(returnValue(parser));
            oneOf(parser).readValueAs(clazz);
            will(returnValue(input));
        }
    };
}
Also used : Expectations(org.jmock.Expectations) JsonFactory(com.fasterxml.jackson.core.JsonFactory) JsonParser(com.fasterxml.jackson.core.JsonParser)

Aggregations

JsonFactory (com.fasterxml.jackson.core.JsonFactory)266 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)102 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)91 JsonParser (com.fasterxml.jackson.core.JsonParser)78 Test (org.junit.Test)65 IOException (java.io.IOException)62 StringWriter (java.io.StringWriter)60 Map (java.util.Map)27 HashMap (java.util.HashMap)26 ArrayList (java.util.ArrayList)25 JsonNode (com.fasterxml.jackson.databind.JsonNode)21 List (java.util.List)18 ExtensibleJSONWriter (com.instagram.common.json.annotation.processor.support.ExtensibleJSONWriter)16 JsonToken (com.fasterxml.jackson.core.JsonToken)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)15 File (java.io.File)14 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)9 InputStreamReader (java.io.InputStreamReader)9 TypeReference (com.fasterxml.jackson.core.type.TypeReference)8 SimpleParseUUT (com.instagram.common.json.annotation.processor.uut.SimpleParseUUT)8