Package dev.javatools.json
Class SortJson
java.lang.Object
dev.javatools.json.SortJson
public class SortJson
extends java.lang.Object
This class has all the apis needs to sort a Json String.
How to use listKeys?
If the json has a list of objects and if you need to sort the list based on a
field of an object, this feature will help.
Here is how it works.
Take the below Json as an example
{
"members": [
{
"name": "John"
"age": 22
}
{
"name": "Bob"
"age": 18
}
]
}
If you need the sort the above Json based on the member name. You need to pass
listKey.put("members[]", "name");
If you need the sort the above Json based on the member age. You need to pass
listKey.put("members[]", "age");
-
Constructor Summary
Constructors Constructor Description SortJson() -
Method Summary
Modifier and Type Method Description java.lang.StringgetSortedJson(java.io.File input)Sorts the given json String and respond the sorted and formatted String.java.lang.StringgetSortedJson(java.io.File input, java.util.Map<java.lang.String,java.lang.String> listKeys)Sorts the given json String and respond the sorted and formatted String.java.lang.StringgetSortedJson(java.lang.Object input)Sorts the given json String and respond the sorted and formatted String.java.lang.StringgetSortedJson(java.lang.Object input, java.util.Map<java.lang.String,java.lang.String> listKeys)Sorts the given json String and respond the sorted and formatted String.java.lang.StringgetSortedJson(java.lang.String input)Sorts the given json String and respond the sorted and formatted String.java.lang.StringgetSortedJson(java.lang.String input, java.util.Map<java.lang.String,java.lang.String> listKeys)Sorts the given json String and respond the sorted and formatted String.java.lang.StringgetSortedJson(java.util.Map<java.lang.String,java.lang.Object> input)Sorts the given json String and respond the sorted and formatted String.
-
Constructor Details
-
SortJson
public SortJson()
-
-
Method Details
-
getSortedJson
public java.lang.String getSortedJson(java.lang.String input) throws com.fasterxml.jackson.core.JsonProcessingExceptionSorts the given json String and respond the sorted and formatted String.- Parameters:
input- Json String- Returns:
- returns the formatted and sorted json in String representation
- Throws:
com.fasterxml.jackson.core.JsonProcessingException- Throws JsonProcessingException, if the provided Json is not valid
-
getSortedJson
public java.lang.String getSortedJson(java.lang.String input, java.util.Map<java.lang.String,java.lang.String> listKeys) throws com.fasterxml.jackson.core.JsonProcessingExceptionSorts the given json String and respond the sorted and formatted String.- Parameters:
input- Json StringlistKeys- List of field names that needs to be sorted, see the class documentation for more details- Returns:
- returns the formatted and sorted json in String representation
- Throws:
com.fasterxml.jackson.core.JsonProcessingException- Throws JsonProcessingException, if the provided Json is not valid
-
getSortedJson
public java.lang.String getSortedJson(java.io.File input) throws java.io.IOExceptionSorts the given json String and respond the sorted and formatted String.- Parameters:
input- Json String- Returns:
- returns the formatted and sorted json in String representation
- Throws:
java.io.IOException- Throws IOException, if the provided Json is not valid or if the file does not exists.
-
getSortedJson
public java.lang.String getSortedJson(java.io.File input, java.util.Map<java.lang.String,java.lang.String> listKeys) throws java.io.IOExceptionSorts the given json String and respond the sorted and formatted String.- Parameters:
input- Json StringlistKeys- List of field names that needs to be sorted, see the class documentation for more details- Returns:
- returns the formatted and sorted json in String representation
- Throws:
java.io.IOException- Throws IOException, if the provided Json is not valid or if the file does not exists.
-
getSortedJson
public java.lang.String getSortedJson(java.lang.Object input) throws java.io.IOExceptionSorts the given json String and respond the sorted and formatted String.- Parameters:
input- Json String- Returns:
- returns the formatted and sorted json in String representation
- Throws:
java.io.IOException- Throws IOException, if the provided Json is not valid or if the file does not exists.
-
getSortedJson
public java.lang.String getSortedJson(java.lang.Object input, java.util.Map<java.lang.String,java.lang.String> listKeys) throws java.io.IOExceptionSorts the given json String and respond the sorted and formatted String.- Parameters:
input- Json StringlistKeys- List of field names that needs to be sorted, see the class documentation for more details- Returns:
- returns the formatted and sorted json in String representation
- Throws:
java.io.IOException- Throws IOException, if the provided Json is not valid or if the file does not exists.
-
getSortedJson
public java.lang.String getSortedJson(java.util.Map<java.lang.String,java.lang.Object> input) throws com.fasterxml.jackson.core.JsonProcessingExceptionSorts the given json String and respond the sorted and formatted String.- Parameters:
input- Json String- Returns:
- returns the formatted and sorted json in String representation
- Throws:
com.fasterxml.jackson.core.JsonProcessingException- Throws JsonProcessingException, if the provided Json is not valid
-