Java Json readMandatoryDateField(JsonParser jsonParser, String fieldName)

Here you can find the source of readMandatoryDateField(JsonParser jsonParser, String fieldName)

Description

read Mandatory Date Field

License

Open Source License

Declaration

public static Date readMandatoryDateField(JsonParser jsonParser, String fieldName) throws IOException 

Method Source Code

//package com.java2s;
/**//  w w w  . ja  v  a 2s  .c o  m
 * Logspace
 * Copyright (c) 2015 Indoqa Software Design und Beratung GmbH. All rights reserved.
 * This program and the accompanying materials are made available under the terms of
 * the Eclipse Public License Version 1.0, which accompanies this distribution and
 * is available at http://www.eclipse.org/legal/epl-v10.html.
 */

import static com.fasterxml.jackson.core.JsonToken.FIELD_NAME;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.TimeZone;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

public class Main {
    private static final String ISO_8601_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
    private static final String TIMEZONE_UTC = "UTC";

    public static Date readMandatoryDateField(JsonParser jsonParser, String fieldName) throws IOException {
        String value = readMandatoryField(jsonParser, fieldName);
        if (value == null) {
            return null;
        }

        return parseDateValue(value);
    }

    public static String readMandatoryField(JsonParser parser, String fieldName) throws IOException {
        prepareToken(parser);

        validateTokenType(parser.getCurrentToken(), FIELD_NAME);
        validateFieldName(parser.getCurrentName(), fieldName);

        String result = parser.nextTextValue();
        consumeToken(parser);
        return result;
    }

    public static Date parseDateValue(String value) {
        try {
            return getTimeFormat().parse(value);
        } catch (ParseException e) {
            throw new IllegalArgumentException("Expected date field of format '" + ISO_8601_DATE_FORMAT
                    + "', but found value '" + value + "'.", e);
        }
    }

    public static void prepareToken(JsonParser parser) throws IOException {
        if (parser.getCurrentToken() == null) {
            parser.nextToken();
        }
    }

    public static void validateTokenType(JsonToken token, JsonToken... expected) {
        for (JsonToken eachExpected : expected) {
            if (token == eachExpected) {
                return;
            }
        }

        if (expected.length == 1) {
            throw new IllegalArgumentException("Expected token of type " + expected[0] + ", but found " + token);
        }

        throw new IllegalArgumentException(
                "Expected token of type " + Arrays.toString(expected) + ", but found " + token);
    }

    public static void validateFieldName(String fieldName, String... expected) {
        for (String eachExpected : expected) {
            if (fieldName.equals(eachExpected)) {
                return;
            }
        }

        if (expected.length == 1) {
            throw new IllegalArgumentException(
                    "Expected field of name '" + expected + "', but found field of name '" + fieldName + "'.");
        }

        throw new IllegalArgumentException("Expected field of name '" + Arrays.toString(expected)
                + "', but found field of name '" + fieldName + "'.");
    }

    public static void consumeToken(JsonParser parser) {
        parser.clearCurrentToken();
    }

    private static DateFormat getTimeFormat() {
        DateFormat df = new SimpleDateFormat(ISO_8601_DATE_FORMAT);
        df.setTimeZone(TimeZone.getTimeZone(TIMEZONE_UTC));
        return df;
    }
}

Related

  1. newObject()
  2. nullable(String value)
  3. objectToJsonDateSerializer(Object ts, final String dateformat)
  4. printJavaxJsonProvider()
  5. read(String jsonString)
  6. requiredParams(JsonObject map, String... params)
  7. toDouble(Object input)
  8. toJsonArrayString(Iterator items)
  9. union(JsonObject... values)