Comments (6)
@evan-king The reason to add something to the library is performance - the pointer in the wrapper above will be parsed and processed twice.
from json-pointer.
I'm ok with returning undefined instead of throwing an exception. We should increase the version and add a note about backwards compatibility
from json-pointer.
I think breaking backward compatibility is not a good idea, given how many packages use json-pointer. I already have tests that expect this exception. I think others will have a problem too, even if it's a major version change.
I would look at the option(s) in get, set, remove and api methods. I will think a bit and suggest something.
from json-pointer.
Throwing error is really annoying. I think add a new option is just for backward compatibility, e.g.
api.get = function get (obj, pointer, silent) {
.....
if(!silent) throw new Error('Invalid reference token: ' + tok);
....
}
For old code, silent===undefined
, and will throw error;
For new code, author can pass silent=true
, and get undefined.
It's the same for get
, remove
, parse
.
Hoping a version bump!
from json-pointer.
Having to deal with thrown errors is not ideal for performance or brevity, nor necessarily is the vagueness of getting undefined
and not knowing if the content did not exist or existed with value undefined
.
However users have the necessary power as-is to efficiently provide the desired behavior for themselves. If you want silent failure, just write a wrapper function incorporating api.has()
:
function safeGet(obj, pointer) {
return api.has(obj, pointer)
? api.get(obj, pointer)
: undefined;
}
I'm not sure that adding bloat to the underlying library is warranted.
from json-pointer.
I would prefer if get
does not throw an error in case the property does not exist... As well as for performance and practical reasons. I also think that this is a case that happens quite a lot, and therefore is not an exceptional case (as throwing an exception should be).
from json-pointer.
Related Issues (14)
- Add thrown exceptions in documentation
- Got a license for this? HOT 1
- json-ponter "/" throws "Invalid reference token: " HOT 1
- The pointer.dict function crashes when object contains an array HOT 3
- Switch to the new travis platform HOT 1
- Support relative pointers?
- Missing remove() HOT 3
- CVE-2021-23820 HOT 6
- Selector support and docs
- NVM: duplicate of below. (CVE-2021-23820)
- compile([]) returns "/" HOT 1
- #dict is not working with empty arrays HOT 8
- Funky whitespace in api.walk
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-pointer.