Starcounter-Jack / JSON-Patch

Lean and mean Javascript implementation of the JSON-Patch standard (RFC 6902). Update JSON documents using delta patches.
MIT License
1.82k stars 215 forks source link

jsonpatch.generate $$hashKey #203

Closed RobinsonSir closed 6 years ago

RobinsonSir commented 6 years ago
var jsonObj = 
{
    "id": 22,
    "prefix": "0003",
    "name": "DtType-23-1",
    "isMultichannel": true,
    "needCalibrate": false,
    "info": "xa",
    "icon": "",
    "dtTypeEpTypes": [{
        "epTypeID": 1,
        "channelNo": 0
    }, {
        "epTypeID": 7,
        "channelNo": 1
    }]
}

I use jsonpatch observer to watch the above jsonObj, then i add one obj to jsonObj.dtTypeEpTypes, jsonObj like this

jsonObj =
{
    "id": 22,
    "prefix": "0003",
    "name": "DtType-23-1",
    "isMultichannel": true,
    "needCalibrate": false,
    "info": "xa",
    "icon": "",
    "dtTypeEpTypes": [{
        "epTypeID": 1,
        "channelNo": 0
    }, {
        "epTypeID": 7,
        "channelNo": 1
    }, {
        "epTypeID": 1,
        "channelNo": 2
    }]
}

Then I use jsonPatch generate function, get patch operation array below:

[{
    "op": "add",
    "path": "/dtTypeEpTypes/1/$$hashKey",
    "value": "object:231"
}, {
    "op": "add",
    "path": "/dtTypeEpTypes/0/$$hashKey",
    "value": "object:230"
}, {
    "op": "add",
    "path": "/dtTypeEpTypes/2",
    "value": {
                "epTypeID": 1,
        "channelNo": 2      
    }
}]

As u can see, it contains $$hashKey, i don't know why contains that

alshakero commented 6 years ago

Hello :)

Could you please provide more details?

miyconst commented 6 years ago

@RobinsonSir 请您说清楚一点,您的问题是什么?

RobinsonSir commented 6 years ago

Nobody.......

miyconst commented 6 years ago

@RobinsonSir please help us help you:

  1. Format the code and the text you write. (See what I did with the main comment).
  2. Prepare a jsfiddle which demonstrates the problem.
alshakero commented 6 years ago

I assume you're using this with Angular. You see Angular adds these props to the object for internal diffing. JSON-Patch itself isn't resposible for these.

I can think of two choices, either use Angular's toJson to get your JSON instead of JSON.stringify. More details on this here.


Another solution is to filter patches and remove Angular-added props. Like this:

const patches = [{
    "op": "add",
    "path": "/dtTypeEpTypes/1/$$hashKey",
    "value": "object:231"
}, {
    "op": "add",
    "path": "/dtTypeEpTypes/0/$$hashKey",
    "value": "object:230"
}, {
    "op": "add",
    "path": "/dtTypeEpTypes/2",
    "value": {
                "epTypeID": 1,
        "channelNo": 2      
    }
}]

const filterdPatches = patches.filter(p => !p.path.includes('$$hashKey'));
console.log(filterdPatches);

// filterdPatches
[{"op":"add","path":"/dtTypeEpTypes/2","value":{"epTypeID":1,"channelNo":2}}]

But I personally prefer the first one.

RobinsonSir commented 6 years ago

Yes, you are right. I use Angularjs ng-repeat with jsonObj. Now, i remove $$hashkey by myself. Another solution i will try, thanks very much.

alshakero commented 6 years ago

Great. Closing as answered :)