All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.activej.json.JsonUtils Maven / Gradle / Ivy

The newest version!
package io.activej.json;

import com.dslplatform.json.DslJson;
import com.dslplatform.json.JsonReader;
import com.dslplatform.json.JsonWriter;
import com.dslplatform.json.ParsingException;
import com.dslplatform.json.runtime.Settings;
import io.activej.common.exception.MalformedDataException;

import java.io.IOException;

import static java.nio.charset.StandardCharsets.UTF_8;

public class JsonUtils {
	private static final DslJson DSL_JSON = new DslJson<>(Settings.withRuntime().includeServiceLoader());
	private static final ThreadLocal WRITERS = ThreadLocal.withInitial(DSL_JSON::newWriter);
	private static final ThreadLocal> READERS = ThreadLocal.withInitial(DSL_JSON::newReader);

	public static  String toJson(JsonCodec codec, T object) {
		return toJsonWriter(codec, object).toString();
	}

	public static  byte[] toJsonBytes(JsonCodec codec, T object) {
		return toJsonWriter(codec, object).toByteArray();
	}

	public static  JsonWriter toJsonWriter(JsonCodec codec, T object) {
		JsonWriter jsonWriter = WRITERS.get();
		jsonWriter.reset();
		codec.write(jsonWriter, object);
		return jsonWriter;
	}

	public static  T fromJson(JsonCodec codec, String json) throws MalformedDataException {
		return fromJsonBytes(codec, json.getBytes(UTF_8));
	}

	public static  T fromJsonBytes(JsonCodec codec, byte[] bytes) throws MalformedDataException {
		JsonReader jsonReader = READERS.get().process(bytes, bytes.length);
		return fromJsonReader(codec, jsonReader);
	}

	private static  T fromJsonReader(JsonCodec codec, JsonReader jsonReader) throws MalformedDataException {
		try {
			jsonReader.getNextToken();
			T deserialized = codec.read(jsonReader);
			if (jsonReader.length() != jsonReader.getCurrentIndex()) {
				String unexpectedData = jsonReader.toString().substring(jsonReader.getCurrentIndex());
				throw new MalformedDataException("Unexpected JSON data: " + unexpectedData);
			}
			return deserialized;
		} catch (ParsingException | JsonValidationException e) {
			throw new MalformedDataException(e);
		} catch (IOException e) {
			throw new AssertionError(e);
		}
	}



}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy