Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
MAPson, CSVson library represents JSON as MAP with key as Json-Path and CSV based JSON representation. MAPson provides options to work
json as MAP and Customized CSV.
/*
* Copyright (c) 2020. Virtualan Software Contributors (https://virtualan.io)
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package io.virtualan.mapson;
import io.virtualan.mapson.exception.BadInputDataException;
import io.virtualan.util.Helper;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* MAPson library represents JSON as MAP with key as Json-Path. MAPson provides options to work json
* as MAP. It removes technical dependency between gherkin and Json. This would help lot more for
* Product Owner/Business analysts(Non technical team members) can create a features without knowing
* the details and simply using JSON hierarchy.
*/
@Slf4j
public class Mapson {
private Mapson() {
}
/**
* Build JSON string from Json-Path as key value pair. Usage refer read me file and test code for
* examples
*
* @param jsonPathMap the json path as key value pair
* @return the json string
* @throws BadInputDataException the bad input data exception
*/
public static String buildMAPsonAsJson(Map jsonPathMap)
throws BadInputDataException {
Map params = new LinkedHashMap<>();
for (Map.Entry mapsonEntry : jsonPathMap.entrySet()) {
String key = mapsonEntry.getKey();
if (key.indexOf('.') != -1) {
buildChildJson(params, key.split("\\."), mapsonEntry.getValue());
} else if (key.contains("[") && key.contains("]")) {
String originalKey = (key.indexOf(".") == -1) ? key.substring(0, key.indexOf("[")) : key;
params.put(originalKey, buildObjectList(params, key, mapsonEntry.getValue()));
} else {
params.put(key, mapsonEntry.getValue());
}
}
return buildJsonString(params).toString();
}
/**
* Build JSON string from Json-Path as key value pair. Usage refer read me file and test code for
* examples
*
* @param jsonPathMap the json path as key value pair
* @return the json string
* @throws BadInputDataException the bad input data exception
*/
public static JSONObject buildMAPsonAsJsonObject(Map jsonPathMap)
throws BadInputDataException {
Map params = new LinkedHashMap<>();
for (Map.Entry mapsonEntry : jsonPathMap.entrySet()) {
String key = mapsonEntry.getKey();
if (key.indexOf('.') != -1) {
buildChildJson(params, key.split("\\."), mapsonEntry.getValue());
} else if (key.contains("[") && key.contains("]")) {
String originalKey = (key.indexOf(".") == -1) ? key.substring(0, key.indexOf("[")) : key;
params.put(originalKey, buildObjectList(params, key, mapsonEntry.getValue()));
} else {
params.put(key, mapsonEntry.getValue());
}
}
return buildJsonString(params);
}
/**
* Build JSON string from Json-Path as key value pair. Context value could be replaced in the JSON
* structure. Usage refer read me file and test code for examples
*
* @param jsonPathMap the json path as key value pair
* @param contextObject the context object
* @return json as string
* @throws BadInputDataException the bad input data exception
*/
public static String buildMAPsonAsJson(Map jsonPathMap,
Map contextObject) throws BadInputDataException {
Map params = new LinkedHashMap<>();
for (Map.Entry mapsonEntry : jsonPathMap.entrySet()) {
String key = mapsonEntry.getKey();
if (key.indexOf('.') != -1) {
buildChildJson(params, key.split("\\."), Helper
.getActualValue(mapsonEntry.getValue(), contextObject));
} else if (key.contains("[") && key.contains("]")) {
String elementAt = key.substring(0, key.indexOf('['));
params.put(elementAt, buildObjectList(params, key,
Helper.getActualValue(mapsonEntry.getValue(), contextObject)));
} else {
params.put(key, Helper.getActualValueForAll(mapsonEntry.getValue(), contextObject));
}
}
JSONObject object =buildJsonString(params);
if(object.optJSONArray("") != null){
return ((JSONArray) object.get("")).toString(2);
}
return object.toString(2);
}
public static Object getJSON(String json) {
try {
return new JSONObject(json);
} catch (JSONException err) {
try {
return new JSONArray(json);
} catch (Exception e) {
log.error("invalid JSON > " + json);
}
}
return null;
}
/**
* Build MAPson(Json-Path as key value pair) from json string. Usage refer read me file and test
* code for examples
*
* @param json the json
* @return the map
*/
public static Map buildMAPsonFromJson(String json) {
Object object = getJSON(json);
Map mapsonMap = new LinkedHashMap<>();
if (object != null) {
if (object instanceof JSONArray) {
getJSONPath(object, "", mapsonMap);
} else {
getJSONPath(object, null, mapsonMap);
}
}
return mapsonMap;
}
private static List