Class ArraySizeComparator

  • All Implemented Interfaces:
    JSONComparator

    public class ArraySizeComparator
    extends DefaultComparator
    A JSONAssert array size comparator.

    Some typical usage idioms are listed below.

    Assuming JSON to be verified is held in String variable ARRAY_OF_JSONOBJECTS and contains:

    {a:[7, 8, 9]}

    then:

    To verify that array 'a' contains 3 elements:

    JSONAssert.assertEquals("{a:[3]}", ARRAY_OF_JSONOBJECTS, new ArraySizeComparator(JSONCompareMode.LENIENT));

    To verify that array 'a' contains between 2 and 6 elements:

    JSONAssert.assertEquals("{a:[2,6]}", ARRAY_OF_JSONOBJECTS, new ArraySizeComparator(JSONCompareMode.LENIENT));
    Author:
    Duncan Mackinder
    • Constructor Detail

      • ArraySizeComparator

        public ArraySizeComparator​(JSONCompareMode mode)
        Create new ArraySizeComparator.
        Parameters:
        mode - comparison mode, has no impact on ArraySizeComparator but is used by instance of superclass DefaultComparator to control comparison of JSON items other than arrays.
    • Method Detail

      • compareJSONArray

        public void compareJSONArray​(String prefix,
                                     org.json.JSONArray expected,
                                     org.json.JSONArray actual,
                                     JSONCompareResult result)
                              throws org.json.JSONException
        Expected array should consist of either 1 or 2 integer values that define maximum and minimum valid lengths of the actual array. If expected array contains a single integer value, then the actual array must contain exactly that number of elements.
        Specified by:
        compareJSONArray in interface JSONComparator
        Overrides:
        compareJSONArray in class DefaultComparator
        Parameters:
        prefix - the path in the json where the comparison happens
        expected - the expected JSON array
        actual - the actual JSON array
        result - stores the actual state of the comparison result
        Throws:
        org.json.JSONException - JSON parsing error