com.cedarsoftware.util.io.JsonReader Java Examples

The following examples show how to use com.cedarsoftware.util.io.JsonReader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HandlebarsScriptEngine.java    From sling-samples with Apache License 2.0 6 votes vote down vote up
/** We might do this with a BindingsValuesProvider? */
private Map<?, ?> getData(Resource r) throws ScriptException {
    // Request resource.json and convert the result to Maps
    String jsonString = null;
    try {
        jsonString =
            new ServletInternalRequest(factory.getServletResolver(), r)
            .withExtension("json")
            .execute()
            .getResponseAsString()
        ;
    } catch(Exception e) {
        final ScriptException up = new ScriptException("Internal request failed");
        up.initCause(e);
        log.info("getData() failed", up);
        throw up;
    }
    final Map<?, ?> result = JsonReader.jsonToMaps(jsonString);
    log.debug("getData() returns a Map with {} keys", result.keySet().size());
    return result;
}
 
Example #2
Source File: JsonIoConfig.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void setupJsonReader() {
    JsonReader.assignInstantiator("com.google.common.collect.RegularImmutableBiMap", new MapFactory());
    JsonReader.assignInstantiator("com.google.common.collect.RegularImmutableMap", new MapFactory());
    JsonReader.assignInstantiator("com.google.common.collect.EmptyImmutableBiMap", new MapFactory());
    JsonReader.assignInstantiator("com.google.common.collect.SingletonImmutableBiMap", new MapFactory());
    JsonReader.assignInstantiator("java.util.Collections$EmptyMap", new MapFactory());
    JsonReader.assignInstantiator("java.util.Collections$SingletonMap", new MapFactory());

    JsonReader.assignInstantiator("com.google.common.collect.SingletonImmutableList", new CollectionFactory());
    JsonReader.assignInstantiator("com.google.common.collect.RegularImmutableList", new CollectionFactory());
    JsonReader.assignInstantiator("java.util.Collections$EmptyList", new CollectionFactory());
    JsonReader.assignInstantiator("java.util.Collections$SingletonList", new CollectionFactory());

    JsonReader.assignInstantiator("com.google.common.collect.RegularImmutableSet", new CollectionFactory());
    JsonReader.assignInstantiator("java.util.Collections$EmptySet", new CollectionFactory());
    JsonReader.assignInstantiator("java.util.Collections$SingletonSet", new CollectionFactory());
}
 
Example #3
Source File: TSplunkEventCollectorWriter.java    From components with Apache License 2.0 6 votes vote down vote up
private void handleResponse(String jsonResponseString) throws IOException {
    if (jsonResponseString == null || jsonResponseString.trim().isEmpty()) {
        throw new IOException(getMessage("error.emptyResponse"));
    }
    try {
        // JSONParser jsonParser = new JSONParser();
        JsonObject<Object, Object> json = (JsonObject<Object, Object>) JsonReader.jsonToJava(jsonResponseString);
        LOGGER.debug("Response String:/r/n" + String.valueOf(json));
        lastErrorCode = ((Long) json.get("code")).intValue();
        lastErrorMessage = (String) json.get("text");
        if (lastErrorCode != 0) {
            throw new IOException(getMessage("error.codeMessage", lastErrorCode, lastErrorMessage));
        }
        successCount += splunkObjectsForBulk.size();
    } catch (JsonIoException e) {
        throw new IOException(getMessage("error.responseParseException", e.getMessage()));
    }
}
 
Example #4
Source File: ClientsJsonProvider.java    From java-json-benchmark with MIT License 6 votes vote down vote up
public ClientsJsonProvider() {

        jsonioStreamOptions.put(JsonReader.USE_MAPS, true);
        jsonioStreamOptions.put(JsonWriter.TYPE, false);

        // set johnson JsonReader (default is `JsonProvider.provider()`)
        javax.json.spi.JsonProvider johnzonProvider = new JsonProviderImpl();
        johnzon = new org.apache.johnzon.mapper.MapperBuilder()
            .setReaderFactory(johnzonProvider.createReaderFactory(Collections.emptyMap()))
            .setGeneratorFactory(johnzonProvider.createGeneratorFactory(Collections.emptyMap()))
            .setAccessModeName("field") // default is "strict-method" which doesn't work nicely with public attributes
            .build();

        TypeConverterManager joddTypeConverterManager = TypeConverterManager.get();
        joddTypeConverterManager.register(UUID.class, (TypeConverter<UUID>) value -> UUID.fromString((String)value));
        joddTypeConverterManager.register(LocalDate.class, (TypeConverter<LocalDate>) value -> LocalDate.parse((String)value));
        joddTypeConverterManager.register(OffsetDateTime.class, (TypeConverter<OffsetDateTime>) value -> OffsetDateTime.parse((String)value));

    }
 
Example #5
Source File: TerminationTriggerService.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private boolean isRunningFlowForced(FlowLog fl) {
    Class<?> payloadType = fl.getPayloadType();
    if (TerminationEvent.class.equals(payloadType)) {
        TerminationEvent payload = (TerminationEvent) JsonReader.jsonToJava(fl.getPayload());
        return Boolean.TRUE.equals(payload.getForced());
    } else {
        LOGGER.warn("Payloadtype [{}] is not 'TerminationEvent' for flow [{}]", fl.getPayloadType(), fl.getFlowId());
        return false;
    }
}
 
Example #6
Source File: FlowChainHandler.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public void restoreFlowChain(String flowChainId) {
    Optional<FlowChainLog> chainLog = flowLogService.findFirstByFlowChainIdOrderByCreatedDesc(flowChainId);
    if (chainLog.isPresent()) {
        Queue<Selectable> chain = (Queue<Selectable>) JsonReader.jsonToJava(chainLog.get().getChain());
        flowChains.putFlowChain(flowChainId, chainLog.get().getParentFlowChainId(), chain);
        if (chainLog.get().getParentFlowChainId() != null) {
            restoreFlowChain(chainLog.get().getParentFlowChainId());
        }
    }
}
 
Example #7
Source File: FlowChainLogService.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
public boolean hasEventInFlowChainQueue(List<FlowChainLog> flowChains) {
    Map<String, List<FlowChainLog>> byFlowChainId = flowChains.stream()
            .collect(Collectors.groupingBy(FlowChainLog::getFlowChainId, toList()));
    return byFlowChainId.entrySet().stream().anyMatch(entry -> {
        FlowChainLog latestFlowChain = entry.getValue()
                .stream()
                .sorted(comparing(FlowChainLog::getCreated).reversed())
                .findFirst()
                .get();
        LOGGER.debug("Checking if chain with id {} has any event in it's queue", latestFlowChain.getFlowChainId());
        Queue<Selectable> chain = (Queue<Selectable>) JsonReader.jsonToJava(latestFlowChain.getChain());
        return !chain.isEmpty();
    });
}
 
Example #8
Source File: ETL.java    From WhiteRabbit with Apache License 2.0 5 votes vote down vote up
/**
 * Create an ETL instance by reading a saved file
 * 
 * @param filename
 *            path of the file
 * @param format
 *            [[FileFormat.Binary]] or [[FileFormat.Json]]
 * @return resulting ETL object
 */
public static ETL fromFile(String filename, FileFormat format) {
	ETL result = null;
	try {
		switch (format) {
			case Binary:
				try (FileInputStream fileInputStream = new FileInputStream(filename);
						GZIPInputStream gzipInputStream = new GZIPInputStream(fileInputStream);
						ObjectInputStream objectInputStream = new ObjectInputStream(gzipInputStream)) {
					result = (ETL) objectInputStream.readObject();
				}
				break;
			case GzipJson:
				try (FileInputStream fileInputStream = new FileInputStream(filename);
						GZIPInputStream gzipInputStream = new GZIPInputStream(fileInputStream);
						JsonReader jr = new JsonReader(gzipInputStream)) {
					result = (ETL) jr.readObject();
				}
				break;
			case Json:
				try (FileInputStream fileInputStream = new FileInputStream(filename); JsonReader jr = new JsonReader(fileInputStream)) {
					result = (ETL) jr.readObject();
				}
				break;
		}
		result.filename = filename;
	} catch (Exception exception) {
		exception.printStackTrace();
	}
	return result;
}
 
Example #9
Source File: BeanUserManager.java    From drftpd with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Lowest level method for loading a User object.
 *
 * @param userName The user name to load
 * @throws UserFileException, if an error (i/o) occured while loading data.
 */
protected User loadUser(String userName) throws UserFileException {
    try (InputStream in = new FileInputStream(getUserFile(userName));
         JsonReader reader = new JsonReader(in)) {
        logger.debug("Loading '{}' Json user data from disk.", userName);
        BeanUser user = (BeanUser) reader.readObject();
        user.setUserManager(this);
        return user;
    } catch (Exception e) {
        throw new UserFileException("Error loading " + userName, e);
    }
}
 
Example #10
Source File: UsersJsonProvider.java    From java-json-benchmark with MIT License 5 votes vote down vote up
public UsersJsonProvider() {
    jacksonAfterburner.registerModule(new AfterburnerModule());

    jsonioStreamOptions.put(JsonReader.USE_MAPS, true);
    jsonioStreamOptions.put(JsonWriter.TYPE, false);

    // set johnson JsonReader (default is `JsonProvider.provider()`)
    javax.json.spi.JsonProvider johnzonProvider = new JsonProviderImpl();
    johnzon = new org.apache.johnzon.mapper.MapperBuilder()
        .setReaderFactory(johnzonProvider.createReaderFactory(Collections.emptyMap()))
        .setGeneratorFactory(johnzonProvider.createGeneratorFactory(Collections.emptyMap()))
        .setAccessModeName("field") // default is "strict-method" which doesn't work nicely with public attributes
        .build();
}
 
Example #11
Source File: BeanUserManager.java    From drftpd with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Lowest level method for loading a Group object.
 *
 * @param groupName The group name to load
 * @throws GroupFileException, if an error (i/o) occured while loading data.
 */
protected Group loadGroup(String groupName) throws GroupFileException {
    try (InputStream in = new FileInputStream(getGroupFile(groupName));
         JsonReader reader = new JsonReader(in)) {
        logger.debug("Loading '{}' Json group data from disk.", groupName);
        BeanGroup group = (BeanGroup) reader.readObject();
        group.setUserManager(this);
        return group;
    } catch (Exception e) {
        throw new GroupFileException("Error loading " + groupName, e);
    }
}
 
Example #12
Source File: BeanUserManager.java    From drftpd with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Lowest level method for loading a User object.
 *
 * @param userName The user name to load
 * @throws UserFileException, if an error (i/o) occured while loading data.
 */
protected User loadUser(String userName) throws UserFileException {
    try (InputStream in = new FileInputStream(getUserFile(userName));
         JsonReader reader = new JsonReader(in)) {
        logger.debug("Loading '{}' Json user data from disk.", userName);
        BeanUser user = (BeanUser) reader.readObject();
        user.setUserManager(this);
        return user;
    } catch (Exception e) {
        throw new UserFileException("Error loading " + userName, e);
    }
}
 
Example #13
Source File: BeanUserManager.java    From drftpd with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Lowest level method for loading a Group object.
 *
 * @param groupName The group name to load
 * @throws GroupFileException, if an error (i/o) occured while loading data.
 */
protected Group loadGroup(String groupName) throws GroupFileException {
    try (InputStream in = new FileInputStream(getGroupFile(groupName));
         JsonReader reader = new JsonReader(in)) {
        logger.debug("Loading '{}' Json group data from disk.", groupName);
        BeanGroup group = (BeanGroup) reader.readObject();
        group.setUserManager(this);
        return group;
    } catch (Exception e) {
        throw new GroupFileException("Error loading " + groupName, e);
    }
}
 
Example #14
Source File: TestGraphComparator.java    From java-util with Apache License 2.0 4 votes vote down vote up
private Object clone(Object source) throws Exception
{
    String json = JsonWriter.objectToJson(source);
    return JsonReader.jsonToJava(json);
}
 
Example #15
Source File: ObservedSolution.java    From jMetalSP with MIT License 4 votes vote down vote up
@Override
public ObservedSolution<T, S> fromJson(String jsonString) {
  return (ObservedSolution<T, S>) JsonReader.jsonToJava(jsonString);
}
 
Example #16
Source File: JsonIO.java    From marshalsec with MIT License 4 votes vote down vote up
@Override
public Object unmarshal ( String data ) throws Exception {
    return JsonReader.jsonToJava(data);
}
 
Example #17
Source File: AlgorithmObservedData.java    From jMetalSP with MIT License 4 votes vote down vote up
@Override
public AlgorithmObservedData fromJson(String jsonString) {

  return (AlgorithmObservedData)JsonReader.jsonToJava(jsonString);
}
 
Example #18
Source File: ObservedValue.java    From jMetalSP with MIT License 4 votes vote down vote up
@Override
public ObservedValue<T> fromJson(String jsonString) {
  return (ObservedValue<T>) JsonReader.jsonToJava(jsonString);
}
 
Example #19
Source File: ClientsJsonProvider.java    From java-json-benchmark with MIT License 4 votes vote down vote up
@Nullable
@Override
public OffsetDateTime fromJson(com.squareup.moshi.JsonReader reader) throws IOException {
    return OffsetDateTime.parse(reader.nextString());
}
 
Example #20
Source File: ClientsJsonProvider.java    From java-json-benchmark with MIT License 4 votes vote down vote up
@Nullable
@Override
public LocalDate fromJson(com.squareup.moshi.JsonReader reader) throws IOException {
    return LocalDate.parse(reader.nextString());
}
 
Example #21
Source File: ClientsJsonProvider.java    From java-json-benchmark with MIT License 4 votes vote down vote up
@Nullable
@Override
public BigDecimal fromJson(com.squareup.moshi.JsonReader reader) throws IOException {
    return new BigDecimal(reader.nextString());
}
 
Example #22
Source File: ClientsJsonProvider.java    From java-json-benchmark with MIT License 4 votes vote down vote up
@Nullable
@Override
public UUID fromJson(com.squareup.moshi.JsonReader reader) throws IOException {
    return UUID.fromString(reader.nextString());
}
 
Example #23
Source File: ObservedIntegerValue.java    From jMetalSP with MIT License 4 votes vote down vote up
@Override
public ObservedIntegerValue fromJson(String jsonString) {
  return (ObservedIntegerValue) JsonReader.jsonToJava(jsonString);
}
 
Example #24
Source File: ObservedDoubleSolution.java    From jMetalSP with MIT License 4 votes vote down vote up
@Override
public ObservedDoubleSolution fromJson(String jsonString) {
  return (ObservedDoubleSolution)JsonReader.jsonToJava(jsonString);
}
 
Example #25
Source File: ObservedDoubleSolutionList.java    From jMetalSP with MIT License 4 votes vote down vote up
@Override
public ObservedDoubleSolutionList fromJson(String jsonString) {
  return (ObservedDoubleSolutionList)JsonReader.jsonToJava(jsonString);
}