# private! protected! const! JSON parse directives

**URL:** <https://es.discourse.group/t/private-protected-const-json-parse-directives/1048>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [October 21, 2021, 7:25am UTC](https://es.discourse.group/t/private-protected-const-json-parse-directives/1048 "2021-10-21T07:25:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Seagat2011](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/seagat2011/32/367_2.png) [@Seagat2011](https://es.discourse.group/u/Seagat2011)\
**Post date:** [October 21, 2021, 7:25am UTC](https://es.discourse.group/t/private-protected-const-json-parse-directives/1048/1 "2021-10-21T07:25:29Z")

</div>

```javascript
private! 
protected! 
const! JSON parse directives

myJSON = { 
    const! Apple:"Macintosh", // read-only!
    protected! Macintosh:"Delicious", // read-only! nocopy!
    private! Delicious:"Always", // noread! nocopy!
    private! { DeliciousAlso:"Always", ALocalOnlyPassword:"SavedLocallyInAFileWhichWillNotBeLoadedAtRuntimeOrTransferedOverTheNetwork"... },
}

myJSON.Apple // Macintosh
myJSON.Macintosh // Delicious
myJSON.Delicious // Error Access of property for read or assign is restricted!

let myVarA = myJSON.Apple // Macintosh
let myVarB = myJSON.Macintosh // null
let myVarC = myJSON.Delicious // Error Access of property for read or assign is restricted!

myVarA = myJSON.Apple = 100 // Error Cannot assign const to a value!
myVarB = myJSON.Macintosh = 100 // null
myVarC = myJSON.Delicious = 100 // Error Access of property for read or assign is restricted!

console.log( myJSON.Apple ) // Macintosh
console.log( myJSON.Macintosh ) // Delicious
console.log( myJSON.Delicious ) // Always 

```

---

<div class="post-metadata">

**Author:** ![lightmare](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/lightmare/32/843_2.png) [@lightmare](https://es.discourse.group/u/lightmare)\
**Post date:** [October 21, 2021, 7:46am UTC](https://es.discourse.group/t/private-protected-const-json-parse-directives/1048/2 "2021-10-21T07:46:39Z")

</div>

> [@Seagat2011](#):
>
> ```javascript
> myJSON.Delicious // Error Access of property for read or assign is restricted!
> 
> console.log( myJSON.Delicious ) // Always 
> 
> ```

If you can't read it, you can't print it.

---

<div class="post-metadata">

**Author:** ![kaizhu256](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/kaizhu256/32/342_2.png) [@kaizhu256](https://es.discourse.group/u/kaizhu256)\
**Post date:** [October 21, 2021, 3:22pm UTC](https://es.discourse.group/t/private-protected-const-json-parse-directives/1048/3 "2021-10-21T15:22:48Z")

</div>

just fyi in real-world-problems, if i want a json-object to be immutable, an effective poorman's solution is to store it in `JSON.stringify`'d form:

```javascript
let readOnlyConfig = JSON.stringify({
    Apple: "Macintosh", // read-only!
    Macintosh: "Delicious" // read-only! nocopy!
});

console.log( JSON.parse(readOnlyConfig).Apple ); // Macintosh
console.log( JSON.parse(readOnlyConfig).Macintosh ); // Delicious

// plus the data is easier to message-pass (which is always a plus in javascript)
postMessage(readOnlyConfig);

```

---

<div class="post-metadata">

**Author:** ![rumkin](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/rumkin/32/598_2.png) [@rumkin](https://es.discourse.group/u/rumkin)\
**Post date:** [October 22, 2021, 4:53am UTC](https://es.discourse.group/t/private-protected-const-json-parse-directives/1048/4 "2021-10-22T04:53:54Z")

</div>

@kaizhu256 There is [Object.freeze](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze) for that. It's more performant, reliable and robust solution.

---

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [October 23, 2021, 12:23am UTC](https://es.discourse.group/t/private-protected-const-json-parse-directives/1048/5 "2021-10-23T00:23:51Z")

</div>

What's the use case (beyond a possible `#key: value` private property for objects)?
