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

com.dslplatform.json.parsers.JsObjParser Maven / Gradle / Ivy

package com.dslplatform.json.parsers;

import com.dslplatform.json.JsonReader;
import com.dslplatform.json.ParsingException;
import io.vavr.collection.HashMap;
import jsonvalues.JsObj;
import jsonvalues.JsValue;
import jsonvalues.spec.Error;

import java.io.IOException;
import java.util.Optional;
import java.util.function.Function;

final class JsObjParser extends AbstractJsObjParser {

    private final JsValueParser valueDeserializer;

    JsObjParser(final JsValueParser valueDeserializer) {
        this.valueDeserializer = valueDeserializer;
    }

    JsObj valueSuchThat(final JsonReader reader,
                        final Function> fn
                       ) throws JsParserException {
        try {
            final JsObj           value  = value(reader);
            final Optional result = fn.apply(value);
            if (!result.isPresent()) return value;
            throw reader.newParseError(result.toString());
        } catch (ParsingException e) {
            throw new JsParserException(e.getMessage());

        }

    }

    @Override
    public JsObj value(final JsonReader reader) throws JsParserException {
        try {
            if (isEmptyObj(reader)) return EMPTY_OBJ;

            String key = reader.readKey();
            JsObj map = EMPTY_OBJ.set(key,
                                      valueDeserializer.value(reader)
                                     );
            byte nextToken;
            while ((nextToken = reader.getNextToken()) == ',') {
                reader.getNextToken();
                key = reader.readKey();
                map = map.set(key,
                              valueDeserializer.value(reader)
                             );
            }
            if (nextToken != '}') throw reader.newParseError("Expecting '}' for map end");
            return map;
        } catch (IOException e) {
            throw new JsParserException(e.getMessage());

        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy