简体   繁体   中英

Best practice - JSON vs JS Object

Consider this simple angular example & navigate to question.service.ts

Snippet from question.service.ts:

new DropdownQuestion({
        key: 'brave',
        label: 'Bravery Rating',
        options: [
          {key: 'solid',  value: 'Solid'},
          {key: 'great',  value: 'Great'},
          {key: 'good',   value: 'Good'},
          {key: 'unproven', value: 'Unproven'}
        ],
        order: 3
      })

Alternate to above (raw json):

{
       key: 'brave',
        label: 'Bravery Rating',
        options: [
          {key: 'solid',  value: 'Solid'},
          {key: 'great',  value: 'Great'},
          {key: 'good',   value: 'Good'},
          {key: 'unproven', value: 'Unproven'}
        ],
        order: 3
      }

What I feel is rather than passing this raw json (as described above), I use the method 1 described above.

I don't have enough arguments to support my point here. But I feel that passing in JS object using the supplied JSON object as parameter is way better than passing raw json, in terms or readability, maintainability & OOP.

I am facing a hard time convincing my fellow colleagues to agree with my approach. What would actually be the best way here? Is passing raw json really a better approach then JS? or vice versa? Is there any alternate way to this?

Thank you in advance.

  • First of all, both of them are completely irrelevant since first option expecting some specific format as input however second option is free data structure which can be feed as input to DropdownQuestion .

  • Second, I am guessing, what you are seeking for is to force the control to have very specific structure rather than wrapping something in it. You DropdownQuestion class should be look like as -

     class DropdownQuestion implements Control{ public key; public label; public order; public options =[]; }

Here Control is common interface that needs to be implemented by all Controls .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM