npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

json-converter

v1.0.0

Published

convert json object. for example from hateoas to jsonapi

Downloads

44

Readme

JsonConverter

Build Status Sauce Test Status Sauce Test Status

convert json object's value. Usually use to convert server response data format, for instance from hateoas to jsonapi, If your server is spring data, and javascript side is Emberjs.

Design

Use visitor pattern.

entry point:

traversal(obj,visitors);

every visitor in visitors's visit method will be called with three parameters.

visit(parent, key, obj);
  • parent, obj's parent object, for top object, it's value is null.
  • key, parent[k] === obj.
  • obj, the obj for you to convert.

as a convention, if your visitor doesn't change json's structure,you can put them as an array to traversal, or else it's better to traverse object separate. any way, you can decide how to implentation.

How to use Hateoas2Jsonapi

convert hateoas response to jsonapi, you must provide extra information to converter. For example, this is a response.

fixtrures/person.js

{
    "id": 1,
    "displayName": "[email protected]",
    "level": 1,
    "roles": [{
      "name": "ROLE_USER",
      "id": 1
    }],
    "_embedded": {
      "creator": {
        "displayName": "[email protected]",
        "id": 3,
        "level": 1,
        "createdAt": "2015-08-29T08:17:29.705+0000",
        "gender": "FEMAIL",
        "avatar": "",
        "roles": [{
          "name": "ROLE_WORKER"
        }, {
          "name": "ROLE_USER"
        }],
        "lastScheduleExec": null,
        "_links": {
          "self": {
            "href": "http://localhost/api/v1/people/3{?projection}",
            "templated": true
          }
        }
      },
      "tasks": [{
        "id": 1,
        "state": "UN_FETCHED",
        "createdAt": "2015-08-29T08:17:29.986+0000",
        "feedbackUrl": "http://localhost/api/v1/tts/",
        "fetchedAt": null,
        "finishedAt": null,
        "_links": {
          "self": {
            "href": "http://localhost/api/v1/tts/1{?projection}",
            "templated": true
          }
        }
      }, {
        "id": 2,
        "state": "UN_FETCHED",
        "createdAt": "2015-08-29T08:17:29.986+0000",
        "sourceFrom": "/local.apk",
        "feedbackUrl": "http://localhost/api/v1/taskitems/",
        "fetchedAt": null,
        "finishedAt": null,
        "_links": {
          "self": {
            "href": "http://localhost/api/v1/taskitems/2{?projection}",
            "templated": true
          }
        }
      }]
    },
    "createdAt": "2015-08-18T01:00:06.723+0000",
    "lastScheduleExec": null,
    "gender": "FEMAIL",
    "avatar": "",
    "email": "[email protected]",
    "ucOpenId": "1b6e5cf7a5f6434a934f9a040b65238a",
    "_links": {
      "self": {
        "href": "http://localhost/api/v1/people/3{?projection}",
        "templated": true
      }
    }
  }

You have no idea of which type of model this response stand for. and you cannot decide what "roles" field mean, in the "_embedded" field, we can guess it, but it's not accurate. So, *** We must identify all the model type the response contains ***, In other word, You must know exactly your app's data structure.

in the test folder, there is a tutils.js, which is a configuration pass to converter.

export function opts() {
  return {
    typePathMap: {
      person: { //for response for person model. /api/people/1, or /api/people
        role: "roles|_embedded/roles", // start from main model, roles, or _embedded.roles may contain role model.
        person: "_embedded/creator",
        task: "_embedded/tasks"
      }
    }
  };
}

Now, in test/hateoas2jsonapi/hateoas2jsonapi-test.js

    let convertor = new Hateoas2Jsonapi(opts());
    let p = person();
    let result = convertor.convert(p, "person"); // for every convert action, pass in the main model name.
    // p is response from server.

For Emberjs Data

I'm not write a ember plugin, instead write a general convertor, you can import the converter in you ember app. just write some hook in adapter.