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

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

package com.dslplatform.json.parsers;

import com.dslplatform.json.JsonReader;
import com.dslplatform.json.MyNumberConverter;
import com.dslplatform.json.ParsingException;
import jsonvalues.JsInt;
import jsonvalues.spec.Error;

import java.util.Optional;
import java.util.function.IntFunction;

final class JsIntParser extends AbstractParser {
    @Override
    JsInt value(final JsonReader reader) throws JsParserException {
        try {
            return JsInt.of(MyNumberConverter.parseInt(reader));
        } catch (ParsingException e) {
            throw new JsParserException(e.getMessage());
        }
    }

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

        }

    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy