All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.minidev.json.actions.PathReplicator Maven / Gradle / Ivy

Go to download

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

There is a newer version: 2.5.1
Show newest version
package net.minidev.json.actions;

import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.actions.navigate.CopyPathsAction;
import net.minidev.json.actions.navigate.JSONNavigator;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * Creates a copy of a {@link JSONObject} consisting only of the nodes on the user-specified paths.
 * 

* Paths that do not exist in the specified object are ignored silently. Specifying an empty list of paths * to copy or only non-existent paths will result in an empty object being returned. *

* A path to copy must be specified in the n-gram format - a list of keys from the root down separated by dots: * K0[[[[.K1].K2].K3]...] *
* A key to the right of a dot is a direct child of a key to the left of a dot. Keys with a dot in their name are * not supported. *

* Sample usage: *

* To replicate the branch k1.k2 from {k1:{k2:v2}, k3:{k4:v4}} use the {@link PathReplicator} like so: *

 * PathReplicator pr = new {@link PathReplicator}("k1.k2")
 * JSONObject copiedObject = pr.copy(new JSONObject(...))
 * 
* The resulting object 'copiedObject' would be {k1:{k2:v2}} *

* see unit tests for more examples * * @author [email protected] * @since 15 March 2016. */ public class PathReplicator { protected List pathsToCopy; public PathReplicator(JSONArray pathsToCopy) { if (pathsToCopy == null || pathsToCopy.isEmpty()) { this.pathsToCopy = Collections.emptyList(); } else { this.pathsToCopy = new LinkedList(); for (Object s : pathsToCopy) { this.pathsToCopy.add((String) s); } } } public PathReplicator(List pathsToCopy) { this.pathsToCopy = pathsToCopy == null || pathsToCopy.size() == 0 ? Collections. emptyList() : pathsToCopy; } public PathReplicator(String... pathsToCopy) { this.pathsToCopy = pathsToCopy == null || pathsToCopy.length == 0 ? Collections. emptyList() : new LinkedList(Arrays.asList(pathsToCopy)); } public JSONObject replicate(JSONObject sourceObj) throws Exception { CopyPathsAction s = new CopyPathsAction(); JSONNavigator n = new JSONNavigator(s, pathsToCopy); n.nav(sourceObj); return (JSONObject) s.result(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy