/*
Accepts JSONObject / JSONArray and returns the Events for the required
permutations.

NOTE: this doesn't check if the objects are actually different, call this
only if an event is to be created for sure, just to find out what events
are to be created.
350294
public static Set<Event> compareAndExtractEvents(String pre, String post, String ids) throws JSONException, IllegalArgumentException {
        Object preObject = null, postObject = null, idsObject = null;
        if (pre != null) {
            preObject = JSONParser.parseJSON(pre);
        }
        if (post != null && ids != null) {
            postObject = JSONParser.parseJSON(post);
            idsObject = JSONParser.parseJSON(ids);
        } else {
            throw new IllegalArgumentException("post state and projected ids cannot be null:: " + getErrorSignature(pre, post, ids));
        }

        if (!(preObject == null || preObject.getClass().equals(postObject.getClass())) || !postObject.getClass().equals(idsObject.getClass())) {
            throw new IllegalArgumentException("pre state (optional), post state and projected ids need to be valid JSON of the same type:: "
                    + getErrorSignature(pre, post, ids));
        }
        // JSONParser only returns JSONArray/ JSONObject/ JSONString
        if (!postObject.getClass().equals(JSONArray.class) && !postObject.getClass().equals(JSONObject.class)) {
            throw new IllegalArgumentException("Identities can only extracted from JSONArrays or JSONObjects:: " + getErrorSignature(pre, post, ids));
        }

        return compareAndGetEvents(preObject, postObject, idsObject);
    }
806255
public function implementedEvents()
    {
        $events = [];
        if ($this->getConfig('events') === false) {
            return $events;
        }
        foreach ((array)$this->getConfig('events') as $eventKey => $event) {
            if (is_numeric($eventKey)) {
                $eventKey = $event;
                $event = null;
            }
            if ($event === null || is_string($event)) {
                $event = ['callable' => $event];
            }
            if (!is_array($event)) {
                throw new \InvalidArgumentException('Event should be string or array');
            }
            $priority = $this->getConfig('priority');
            if (!array_key_exists('callable', $event) || $event['callable'] === null) {
                list(, $event['callable']) = pluginSplit($eventKey);
            }
            if ($priority && !array_key_exists('priority', $event)) {
                $event['priority'] = $priority;
            }
            $events[$eventKey] = $event;
        }

        return $events;
    }
1292787
@Override
    public List<Object> checkForErrors(Object[] objs) {
        List<Object> errors = new ArrayList<Object>();
        if (objs != null) {
            for (int cont = 0; cont < objs.length; cont ++) {
                if (objs[cont] == null) {
                    logger.debug("A operation could not be performed.");
                    errors.add(objs[cont]);
                }
            }
        }
        return errors;
    }
1292218
public JSONArray names() {
      verifyIsNull();
      JSONArray ja = new JSONArray();
      Iterator keys = keys();
      while( keys.hasNext() ){
         ja.element( keys.next() );
      }
      return ja;
   }
622268
public JSONObject getJson() throws JSONException {
        JSONObject json = create();
        json.put("id", getLogicalId());
        json.put("to", "A" + toId);
        if (completionCode != null)
            json.put("resultCode", completionCode);
        if (eventType != null)
            json.put("event", EventType.getEventTypeName(eventType));
        if (attributes != null && ! attributes.isEmpty())
            json.put("attributes", Attribute.getAttributesJson(attributes));
        return json;
    }
1085068
public static Set<Event> compareAndGetEvents(JSONObject pre, JSONObject post, JSONObject ids) throws JSONException {
        Set<Event> result = new HashSet<>();
        Iterator<String> keysIterator = ids.keys();
        while (keysIterator.hasNext()) {
            String key = keysIterator.next();
            if (post.get(key) instanceof JSONObject || post.get(key) instanceof JSONArray) {
                Object preObject = (pre == null) ? null : pre.opt(key);
                Set<Event> events = compareAndGetEvents(preObject, post.get(key), ids.get(key));
                for (Event event : events) {
                    for (Identity identity : event.getIdentity()) {
                        identity.setField(key + "." + identity.getField());
                    }
                }
                result = crossMultiplySets(result, events);
            } else {
                Identity i = new Identity();
                i.setField(key);
                i.setValue(post.get(key).toString());
                Operation operation = (pre == null) ? Operation.INSERT : Operation.UPDATE;
                if (result.size() == 0) {
                    Event event = new Event();
                    event.setOperation(operation);
                    result.add(event);
                }
                for (Event event : result) {
                    event.getIdentity().add(i);
                }
            }
        }
        return result;
    }
1288346
private Object parseObject(
            JsonEvent event,
            Class<?> inputDeclaredType,
            MetaProperty<?> metaProp,
            Class<?> beanType,
            SerIterable parentIterable,
            boolean rootType) throws Exception {

        // avoid nulls
        Class<?> declaredType = (inputDeclaredType == null ? Object.class : inputDeclaredType);
        // establish type
        if (event == JsonEvent.OBJECT) {
            event = input.readEvent();
            if (event == JsonEvent.STRING) {
                String key = input.parseObjectKey();
                if (key.equals(BEAN)) {
                    return parseTypedBean(declaredType, rootType);
                } else if (key.equals(TYPE)) {
                    return parseTypedSimple(declaredType);
                } else if (key.equals(META)) {
                    return parseTypedMeta();
                } else {
                    input.pushBack('"');
                    input.pushBackObjectKey(key);
                    event = JsonEvent.OBJECT;
                }
            } else if (event == JsonEvent.OBJECT_END) {
                input.pushBack('}');
                event = JsonEvent.OBJECT;
            } else {
                throw new IllegalArgumentException("Invalid JSON data: Expected JSON object end but found " + event);
            }
        }
        // parse based on type
        if (Bean.class.isAssignableFrom(declaredType)) {
            if (event == JsonEvent.OBJECT) {
                return parseBean(input.readEvent(), declaredType);
            } else {
                return parseSimple(event, declaredType);
            }
        } else {
            if (event == JsonEvent.OBJECT || event == JsonEvent.ARRAY) {
                SerIterable childIterable = null;
                if (metaProp != null) {
                    childIterable = settings.getIteratorFactory().createIterable(metaProp, beanType, true);
                } else if (parentIterable != null) {
                    childIterable = settings.getIteratorFactory().createIterable(parentIterable);
                }
                if (childIterable == null) {
                    if (event == JsonEvent.ARRAY) {
                        childIterable = parseUnknownArray(declaredType);
                    } else {
                        childIterable = parseUnknownObject(declaredType);
                    }
                }
                return parseIterable(event, childIterable);
            } else {
                return parseSimple(event, declaredType);
            }
        }
    }
156560
private Variants parseEras(JsonObject calendar) {
    JsonObject node = resolve(calendar, "eras");

    JsonObject abbr = resolve(node, "eraAbbr");
    JsonObject narrow = resolve(node, "eraNarrow");
    JsonObject wide = resolve(node, "eraNames");

    return new Variants(
        new String[] { string(abbr, "0"), string(abbr, "1") },
        new String[] { string(narrow, "0"), string(narrow, "1") },
        new String[] { },
        new String[] { string(wide, "0"), string(wide, "1") });
  }
905956
function (obj, eventType, eventHandler) {
			if (!obj.eventHandlers) { obj.eventHandlers = {}; }
			if (!obj.eventHandlers[eventType]) {
				obj.eventHandlers[eventType] = [];
			}
			if (eventHandler) {
				obj.eventHandlers[eventType].push(eventHandler);
			}
			return obj.eventHandlers[eventType];
		}
276370
private void parseEvent(JsonObject event, Parser parser, int i) {

        JsonElement nameJE = event.get(Event.KEY_NAME);

        if (nameJE != null) {

            String name = nameJE.getAsString();

            if (MessageSentEvent.TYPE.equals(name)) {
                MessageSentEvent parsed = parser.parse(event, MessageSentEvent.class);
                messageSent.add(parsed);
                map.put(i, parsed);
            } else if (MessageDeliveredEvent.TYPE.equals(name)) {
                MessageDeliveredEvent parsed = parser.parse(event, MessageDeliveredEvent.class);
                messageDelivered.add(parsed);
                map.put(i, parsed);
            } else if (MessageReadEvent.TYPE.equals(name)) {
                MessageReadEvent parsed = parser.parse(event, MessageReadEvent.class);
                messageRead.add(parsed);
                map.put(i, parsed);
            }
        }
    }
827722
public function getEventsList(int $jd1, int $jd2, string $events, bool $only_living, string $sort_by, Tree $tree): array
    {
        $found_facts = [];
        $facts       = [];

        foreach (range($jd1, $jd2) as $jd) {
            $found_facts = array_merge($found_facts, $this->getAnniversaryEvents($jd, $events, $tree));
        }

        foreach ($found_facts as $fact) {
            $record = $fact->record();
            // only living people ?
            if ($only_living) {
                if ($record instanceof Individual && $record->isDead()) {
                    continue;
                }
                if ($record instanceof Family) {
                    $husb = $record->husband();
                    if ($husb === null || $husb->isDead()) {
                        continue;
                    }
                    $wife = $record->wife();
                    if ($wife === null || $wife->isDead()) {
                        continue;
                    }
                }
            }
            $facts[] = $fact;
        }

        switch ($sort_by) {
            case 'anniv':
                $facts = Fact::sortFacts(Collection::make($facts))->all();
                break;

            case 'alpha':
                uasort($facts, static function (Fact $x, Fact $y): int {
                    return GedcomRecord::nameComparator()($x->record(), $y->record());
                });
                break;
        }

        return $facts;
    }
491241
function pJson(events, top = '') {
    Object.keys(events).forEach((i) => {
      if (typeof events[i] === 'object' && events[i] != null) {
        let rtn;
        if (Object.prototype.toString.call(events) === '[object Array]') {
          rtn = (`${top}[${i}]`);
        } else { rtn = (`${top}.${i}`); }
          pJson(events[i], rtn);
      } else if (Object.prototype.toString.call(events) === '[object Array]') {
        tmp += `{}${top}[${i}] = ${events[i]}\n`;
      } else {
        tmp += `{}${top}.${i} = ${events[i]}\n`;
      }
    });
  }
1295722
@SuppressWarnings("unchecked")
    public static <T> List<T> jsonArrayToList(final JSONArray jsonArray) {
        if (null == jsonArray) {
            return Collections.emptyList();
        }

        final List<T> ret = new ArrayList<T>();

        for (int i = 0; i < jsonArray.length(); i++) {
            ret.add((T) jsonArray.opt(i));
        }

        return ret;
    }
650204
function listeners(type) {
      var object = this,
          events = object.events || (object.events = {});

      return _.has(events, type) ? events[type] : (events[type] = []);
    }
230020
function(jsonOBJ={}){
    // this creates querystring from json object
    var keys = Object.keys(jsonOBJ);
    var queryString = "?";
    for (let index = 0; index < keys.length; index++) {
        if (queryString == "?"){
            queryString += keys[index] + "=" + jsonOBJ[keys[index]];

        }else{
            queryString += "&" + keys[index] + "=" + jsonOBJ[keys[index]];
        }
        
    }
    return queryString;
}
518219
public static JSONArray getArray(JSONValue value) {
        if (value == null)
            return null;
        if (!(value instanceof JSONArray))
            throw new JSONException(NOT_AN_ARRAY);
        return (JSONArray)value;
    }
17054
protected static void fireArrayEndEvent( JsonConfig jsonConfig ) {
      if( jsonConfig.isEventTriggeringEnabled() ){
         for( Iterator listeners = jsonConfig.getJsonEventListeners()
               .iterator(); listeners.hasNext(); ){
            JsonEventListener listener = (JsonEventListener) listeners.next();
            try{
               listener.onArrayEnd();
            }catch( RuntimeException e ){
               log.warn( e );
            }
         }
      }
   }