Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2020 ActiveJ LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.activej.json;
import com.dslplatform.json.BoolConverter;
import com.dslplatform.json.JsonReader;
import com.dslplatform.json.JsonWriter;
import com.dslplatform.json.NumberConverter;
import io.activej.common.annotation.StaticFactories;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.*;
import java.util.function.Function;
import java.util.function.Supplier;
import static io.activej.common.Checks.checkArgument;
import static io.activej.common.Checks.checkNotNull;
import static io.activej.common.Utils.newLinkedHashMap;
import static io.activej.common.Utils.transformIterator;
import static io.activej.json.JsonKeyCodec.ofStringKey;
import static io.activej.json.JsonValidationUtils.validateNotNull;
@SuppressWarnings({"ConstantConditions", "unchecked"})
@StaticFactories(JsonCodec.class)
public class JsonCodecs {
public static JsonCodec ofString() {
return new JsonCodecString();
}
public static JsonCodec ofByte() {
return new JsonCodecByte();
}
public static JsonCodec ofShort() {
return new JsonCodecShort();
}
public static JsonCodec ofInteger() {
return new JsonCodecInteger();
}
public static JsonCodec ofLong() {
return new JsonCodecLong();
}
public static JsonCodec ofFloat() {
return new JsonCodecFloat();
}
public static JsonCodec ofDouble() {
return new JsonCodecDouble();
}
public static JsonCodec ofBoolean() {
return new JsonCodecBoolean();
}
public static JsonCodec ofCharacter() {
return new JsonCodecCharacter();
}
public static JsonCodec ofLocalDate() {
return new JsonCodecLocalDate();
}
private static final class JsonCodecString implements JsonCodec {
@Override
public String read(JsonReader> reader) throws IOException {
return validateNotNull(reader.readString());
}
@Override
public void write(JsonWriter writer, String value) {
writer.writeString(checkNotNull(value));
}
}
private static final class JsonCodecByte implements JsonCodec {
@Override
public Byte read(JsonReader> reader) throws IOException {
int result = NumberConverter.deserializeInt(reader);
if (result < 0 || result > 255) {
throw reader.newParseError("Read an int not in range [0, 255] while trying to read a byte");
}
return (byte) result;
}
@Override
public void write(JsonWriter writer, Byte value) {
NumberConverter.serialize(checkNotNull(value) & 0xFF, writer);
}
}
private static final class JsonCodecShort implements JsonCodec {
@Override
public Short read(JsonReader> reader) throws IOException {
return NumberConverter.deserializeShort(reader);
}
@Override
public void write(JsonWriter writer, Short value) {
NumberConverter.serialize(checkNotNull(value), writer);
}
}
private static final class JsonCodecInteger implements JsonCodec {
@Override
public Integer read(JsonReader> reader) throws IOException {
return NumberConverter.deserializeInt(reader);
}
@Override
public void write(JsonWriter writer, Integer value) {
NumberConverter.serialize(checkNotNull(value), writer);
}
}
private static final class JsonCodecLong implements JsonCodec {
@Override
public Long read(JsonReader> reader) throws IOException {
return NumberConverter.deserializeLong(reader);
}
@Override
public void write(JsonWriter writer, Long value) {
NumberConverter.serialize(checkNotNull(value), writer);
}
}
private static final class JsonCodecFloat implements JsonCodec {
@Override
public Float read(JsonReader> reader) throws IOException {
return NumberConverter.deserializeFloat(reader);
}
@Override
public void write(JsonWriter writer, Float value) {
NumberConverter.serialize(checkNotNull(value), writer);
}
}
private static final class JsonCodecDouble implements JsonCodec {
@Override
public Double read(JsonReader> reader) throws IOException {
return NumberConverter.deserializeDouble(reader);
}
@Override
public void write(JsonWriter writer, Double value) {
NumberConverter.serialize(checkNotNull(value), writer);
}
}
private static final class JsonCodecBoolean implements JsonCodec {
@Override
public Boolean read(JsonReader> reader) throws IOException {
return BoolConverter.deserialize(reader);
}
@Override
public void write(JsonWriter writer, Boolean value) {
BoolConverter.serialize(checkNotNull(value), writer);
}
}
private static final class JsonCodecCharacter implements JsonCodec {
@Override
public Character read(JsonReader> reader) throws IOException {
String string = reader.readString();
if (string.length() != 1) {
throw reader.newParseError("Read a string with length != 1 while trying to read a character");
}
return string.charAt(0);
}
@Override
public void write(JsonWriter writer, Character value) {
writer.writeString(checkNotNull(value).toString());
}
}
private static final class JsonCodecLocalDate implements JsonCodec {
@Override
public LocalDate read(JsonReader> reader) throws IOException {
try {
return LocalDate.parse(validateNotNull(reader.readString()));
} catch (DateTimeParseException e) {
throw reader.newParseError(e.getMessage());
}
}
@Override
public void write(JsonWriter writer, LocalDate value) {
writer.writeString(checkNotNull(value).toString());
}
}
public static > JsonCodec ofEnum(Class enumClass) {
return new JsonCodec<>() {
@Override
public E read(JsonReader> reader) throws IOException {
try {
return Enum.valueOf(enumClass, reader.readString());
} catch (IllegalArgumentException e) {
throw reader.newParseError(e.getMessage());
}
}
@Override
public void write(JsonWriter writer, E value) {
writer.writeString(checkNotNull(value).name());
}
};
}
static JsonCodec