GithubHelp home page GithubHelp logo

jsonmapper's People

Contributors

3d0c avatar ahiipsa avatar alexwhitman avatar alissonperez avatar dregenor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

jsonmapper's Issues

Mapping while array exists in the converter

Hi, thanks for your working, given codes:

const _jmap = require('json-mapper');
const _h = _jmap.helpers;

 const json = { a: 1, b: 2, c: 3 };
const mapping = {
            x: "c", y: "b", z: "a",
            arr: [
                { k: _h.def('m'), v: _h.def(2) },
                { k: _h.def('n'), v: _h.def(4) }
            ]
        };
const conv = _jmap.makeConverter(mapping);
const ret = conv(json);
console.log(ret);

{x:3,y:2,z:1,arr:{k:'n',v:4}}

I hope the arr in the mapping would keep as it was, but it would return just the last element, how to correct the codes?

Computing residue?

Is there a way to use json-mapper to determine the unmapped residue? That is, if I create a converter and I map a bunch of fields from one JSON format into a target format, is there a way to get a list of fields that weren't converted (i.e. dropped) from the input?

Returning an array

Is there any way to run a converter on an array of objects and return an array of converted objects? For example:

var input = [
    {
        a: '1',
        b: '2'
    },
    {
        a: '3',
        b: '4'
    }
];

var objConverter = jm.makeConverter({
    a: 'a'
});

var arrayConverter = jm.makeConverter([
    // I want to map the objConverter to each object in input
]);

arrayConverter(input);

/*
Expected result:

[
    {
        a: '1'
    },
    {
        a: '3'
    }
]
*/

makeConverter modifies its arguments

When you call makeConverter on an object, the implementation of that function actually destructively modifies the source object.

Sample test case to show the issue:

> var jm = require('json-mapper');
undefined
> var myConverter = { 'foo': 'bar', subDocument: { hello: 'world' } };
undefined
> var converter = jm.makeConverter(myConverter);
undefined
> myConverter.foo
[Function]
> myConverter.subDocument
[Function]
> 

The source code that modifies the original object is here

This was the cause of a quite difficult bug; in my application we create mapping classes and we have other uses for those mapping classes besides feeding them into JSON-mapper to create converters. The bug we were seeing was because json-mapper had replaced an object definition (subDocument above) with a function, it's own callback.

Principle of least surprise is that the module should not modify its arguments, instead returning a completely new object leaving the original mapping specification untouched. This makes it possible for us to keep using our data structures as-is.

Our present work-around is to deep clone the objects we have before we give them to json-mapper, so that we can retain a copy without modification.

Multidimensional arrays

Hi,

Is there a way to map out a nested json array, so i can keep references of the child array to its parent while cycling through both?

I've tried using the map feature but within the second map the value passed doesn't seem to quite work.

given an example json:

{
"schools" : [
{
"name" : "school a",
"abbvr" : "AED",
"students" : [
{"name" : "a"},
{"name" : "b"}
]
},
{
"name" : "school b",
"abbvr" : "EG",
"students" : [
{"name" : "c"},
{"name" : "d"}
]
},
{
"name" : "school c",
"abbvr" : "ESA",
"students" : [
{"name" : "e"},
{"name" : "f"}
]
}
]
}

The end goal being to cycle through each school and then cycle through each student as well.

Thanks

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.