Java Json jsonEquals(JsonArray expected, JsonArray actual, boolean strict)

Here you can find the source of jsonEquals(JsonArray expected, JsonArray actual, boolean strict)

Description

json Equals

License

Open Source License

Declaration

public static boolean jsonEquals(JsonArray expected, JsonArray 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:/*from www  .j a  v a 2  s .c  o m*/
 * 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 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 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 Double toDouble(Object input) {
        return ((JsonNumber) input).doubleValue();
    }
}

Related

  1. getAsJSONArray(Object obj)
  2. getFromGson(String json, Class clazz)
  3. getJSONDate(long l)
  4. getJsonMapper()
  5. hasKey(JsonObject object, String name)
  6. jsonObject(String key, JsonValue val)
  7. jsonObjectToBuilder(JsonObject jo)
  8. jsonPrettyPrinter(Object object)
  9. jsonString(JsonValue value)