/*******************************************************************************
 * Copyright (c) 2017 Liviu Ionescu.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Liviu Ionescu - initial version
 *******************************************************************************/

package ilg.gnumcueclipse.core;

import org.json.simple.JSONObject;

public class JsonUtils {

	public static Object get(JSONObject json, String dottedPath) {

		String[] parts = dottedPath.split("[.]");
		Object obj = json;
		for (int i = 0; i < parts.length; ++i) {
			if (obj instanceof JSONObject) {
				obj = ((JSONObject) obj).get(parts[i]);
			}
			if (obj == null) {
				return null;
			}
		}

		return obj;
	}
}

@@ -595,16 +595,178 @@
 tils %7B%0A%0A
+%09/**%0A%09 * Get a JSON property, or the default.%0A%09 * %0A%09 * @param json%0A%09 * @param dottedPath%0A%09 * @param defaultValue%0A%09 * @return an object or the default value.%0A%09 */%0A
 %09public 
@@ -817,16 +817,37 @@
 ttedPath
+, Object defaultValue
 ) %7B%0A%0A%09%09S
@@ -1069,20 +1069,28 @@
 %09return 
-null
+defaultValue
 ;%0A%09%09%09%7D%0A%09
@@ -1109,11 +1109,117 @@
  obj;%0A%09%7D
+%0A%0A%09public static Object get(JSONObject json, String dottedPath) %7B%0A%09%09return get(json, dottedPath, null);%0A%09%7D
 %0A%7D%0A

add get() with default