Java Json compare(JsonValue expected, JsonValue actual, boolean strict)

Here you can find the source of compare(JsonValue expected, JsonValue actual, boolean strict)

Description

compare

License

Open Source License

Declaration

public static boolean compare(JsonValue expected, JsonValue actual, boolean strict) 

Method Source Code

//package com.java2s;
/* Copyright (c) 2017 Boundless and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Distribution License v1.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/org/documents/edl-v10.html
 *
 * Contributors:/* www.ja  v a 2 s  . c om*/
 * Erik Merkle (Boundless) - initial implementation
 * Gabriel Roldan (Boundless) - refactored from TestData to its own class
 */

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.json.JsonArray;
import javax.json.JsonNumber;
import javax.json.JsonObject;

import javax.json.JsonValue;
import javax.json.JsonValue.ValueType;

public class Main {
    public static boolean compare(JsonValue expected, JsonValue actual, boolean strict) {
        switch (expected.getValueType()) {
        case OBJECT:
            if (actual.getValueType() == ValueType.OBJECT) {
                return jsonEquals((JsonObject) expected, (JsonObject) actual, strict);
            }
            break;
        case ARRAY:
            if (actual.getValueType() == ValueType.ARRAY) {
                JsonArray expectedArray = (JsonArray) expected;
                JsonArray actualArray = (JsonArray) actual;
                if (expectedArray.size() != actualArray.size()) {
                    return false;
                }
                if (strict) {
                    for (int i = 0; i < expectedArray.size(); i++) {
                        if (!compare(expectedArray.get(i), actualArray.get(i), strict)) {
                            return false;
                        }
                    }
                } else {
                    List<JsonValue> expectedSet = new LinkedList<JsonValue>();
                    List<JsonValue> actualSet = new LinkedList<JsonValue>();
                    for (int i = 0; i < expectedArray.size(); i++) {
                        expectedSet.add(expectedArray.get(i));
                        actualSet.add(actualArray.get(i));
                    }
                    Iterator<JsonValue> expectedIter = expectedSet.iterator();
                    while (expectedIter.hasNext()) {
                        boolean found = false;
                        JsonValue expectedObject = expectedIter.next();
                        for (JsonValue actualObject : actualSet) {
                            if (compare(expectedObject, actualObject, strict)) {
                                found = true;
                                actualSet.remove(actualObject);
                                break;
                            }
                        }
                        if (!found) {
                            return false;
                        }
                    }
                }
                return true;
            }
            break;
        case STRING:
            if (actual.getValueType() == ValueType.STRING) {
                return expected.equals(actual);
            }
            break;
        case NUMBER:
            if (actual.getValueType() == ValueType.NUMBER) {
                return toDouble(expected).equals(toDouble(actual));
            }
            break;
        default:
            return expected.equals(actual);
        }

        return false;
    }

    public static boolean jsonEquals(JsonArray expected, JsonArray actual, boolean strict) {
        return compare(expected, actual, strict);
    }

    /**
     * Compare two JSON objects. Extra fields are allowed on the actual object if strict is set to
     * {@code false}. Additionally, array ordering does not matter when strict is set to
     * {@code false}.
     *
     * @param expected expected object
     * @param actual actual object
     * @param strict whether or not to perform a strict comparison
     * @return {@code true} if the objects are equal
     */
    public static boolean jsonEquals(JsonObject expected, JsonObject actual, boolean strict) {
        Iterator<String> expectedKeys = expected.keySet().iterator();
        if (strict && expected.size() != actual.size()) {
            return false;
        }
        while (expectedKeys.hasNext()) {
            String key = expectedKeys.next();
            if (!actual.containsKey(key)) {
                return false;
            }
            JsonValue expectedObject = expected.get(key);
            JsonValue actualObject = actual.get(key);
            if (!compare(expectedObject, actualObject, strict)) {
                return false;
            }
        }
        return true;
    }

    public static Double toDouble(Object input) {
        return ((JsonNumber) input).doubleValue();
    }
}

Related

  1. addAll(JsonObjectBuilder a, JsonObjectBuilder b)
  2. asJsonObject(JsonValue value)
  3. asString(JsonValue value)
  4. convertJsonValue(Object jsonValue, Class desiredType)
  5. escape(String text)
  6. fill_dictionary(Map dictionary, JsonObject jsonObject)
  7. fill_key_value(JsonObjectBuilder jsonObject, String key, Object value)