aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/json/readers/JsonParserHelpers.java
blob: 594dfc5ab060fa2d8b7e3f6b9287e1d6a8db06f6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.json.readers;


import com.fasterxml.jackson.core.JsonToken;
import com.google.common.base.Preconditions;

import java.util.Arrays;

public class JsonParserHelpers {

    public static void expectArrayStart(JsonToken token) {
        try {
            Preconditions.checkState(token == JsonToken.START_ARRAY, "Expected start of array, got %s", token);
        }
        catch (IllegalStateException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public static void expectArrayEnd(JsonToken token) {
        try {
            Preconditions.checkState(token == JsonToken.END_ARRAY, "Expected start of array, got %s", token);
        }
        catch (IllegalStateException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public static void expectObjectStart(JsonToken token) {
        try {
            Preconditions.checkState(token == JsonToken.START_OBJECT, "Expected start of JSON object, got %s", token);
        }
        catch (IllegalStateException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public static void expectObjectEnd(JsonToken token) {
        try {
            Preconditions.checkState(token == JsonToken.END_OBJECT, "Expected end of JSON object, got %s", token);
        }
        catch (IllegalStateException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public static void expectCompositeEnd(JsonToken token) {
        try {
            Preconditions.checkState(token.isStructEnd(), "Expected end of composite, got %s", token);
        }
        catch (IllegalStateException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public static void expectScalarValue(JsonToken token) {
        try {
            Preconditions.checkState(token.isScalarValue(), "Expected to be scalar value, got %s", token);
        }
        catch (IllegalStateException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public static void expectOneOf(JsonToken token, JsonToken ... tokens) {
        if (Arrays.stream(tokens).noneMatch(t -> t == token))
            throw new IllegalArgumentException("Expected one of " + tokens + " but got " + token);
    }

}