Giter Club home page Giter Club logo

jmom's Introduction

jmom

Mom is woman, which tries to modify her son to be nicer.

jmom is java library, which tries to modify json to be nicer.

Base of usage

The library is based on antonsjava.json library, which is base for json manipulation. Json data are provided as instances of json API.

Creating json API object from String.

  String jsonvalue = .....
  JsonValue json = JsonParser.parse(jsonvalue);

Creating jmom API object for manipulating json.

  Jmom jmom = Jmom.instance()
            .xxxxx() //a modification rule
            .xxxxx() //a modification rule
            .xxxxx() //a modification rule
            ;

Modifying json using jmom API.

  jmom.apply(json);

Converting json back to String.

  String newjsonvalue = json.toCompactString();

Json data pointers

Modification rules from Jmom API must address some parts of json to be modified. The library uses WildPathMatcher syntax for addressing subparts of json. Individual path elements can be

  • simple names like "foo", "bar"
  • wild names like "foo-*", "bar-?"
  • "**" for representing sequence of path elements

So pointers can look like

  • "/foo/bar/item" represents only "/foo/bar/item"
  • "/foo/*/item" represents paths like "/foo/bar/item", "/foo/nobar/item", ....
  • "/foo/**/item" represents paths like "/foo/item", "/foo/bar/item", "/foo/bar/bar/item"....

Data pointer can be postfixed with '|{int number}'. It moves path selected jsno node to parent {int number} times. For example "/config" selects all paths which ends with '/config' but "/config|1" selects direct parents of previous selections.

Modification rules

Add

Add rule allows to add specified json value to given json. It has following parameters

  • path - exact path where new json must be inserted. (no wilds are allowed here)
  • jsonvalue - value tu be inserted into json
  • replace - true is old value should be replaced is exists. (default: false)

Examples:

   Jmom jmom = Jmom.instance()
     .add("/menu/name", JsonFactory.stringLiteral("nove mneo"), true)
     .add("/menu/popup/menuitem/2", JsonParser.parse("{\"value\": \"SaveAs\", \"onclick\": \"SaveAs()\"}"), true)
   ;

Remove

Remove rule allows to remove specified parts from given json. It has following parameters

  • path - list of data pointers exact path where new json must be inserted.

Examples:

   Jmom jmom = Jmom.instance()
     .remove("/menu/name-?", "**/config|1")
   ;

KeepOnly

KeepOnly rule allows to remove all parts from given json except specified. It has following parameters

  • path - list of data pointers exact path where new json must be saved.

Examples:

   Jmom jmom = Jmom.instance()
     .keepOnly("/menu/name-?", "**/config|1")
   ;

Copy

Copy rule allows to copy specified json part to different place of json. It has following parameters

  • frompath - path which must be copied. (first part is used if more can be identified)
  • topath - exact path where from json must be inserted. (no wilds are allowed here)
  • move - true is old part should be deleted (default: false)

Examples:

   Jmom jmom = Jmom.instance()
     .copy("/menu/id", "/menu/ctx/id", true)
   ;

Apply

Apply rule allows to apply a rule on chosen subparts of given json. The rule can be anything.

  • rule - any rule to be aplied.
  • path - list of data pointers exact path where rule must be applied

Example: (for each objects containing "street" attribute create new one "streetFull" )

   Jmom jmom = Jmom.instance()
     .apply((json) -> {
                String street = json.asObject().first("street");
                String streetnum = json.asObject().first("streetNo");
                json.asObject().attr("streetFull", street+" "+streetNo);
            }, "**/street|1")
   ;

Maven usage

   <dependency>
      <groupId>io.github.antonsjava</groupId>
      <artifactId>jmom</artifactId>
      <version>LASTVERSION</version>
   </dependency>

jmom's People

Contributors

antonsjava avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.