Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ordered table discuss #26

Open
tst2005 opened this issue Jan 9, 2019 · 0 comments
Open

Ordered table discuss #26

tst2005 opened this issue Jan 9, 2019 · 0 comments

Comments

@tst2005
Copy link
Contributor

tst2005 commented Jan 9, 2019

Hello,

I worked for a way to keep the original key order (#4)
@grafi-tt you also worked about it (#25).

Both approach use a special table that remind the keys on the fly during a kind of "learning phase"
after that we are using with pairs to get the keys back in the appropriate order.

My probable usual case will be

  • read a json file (as a config)
  • change a value
  • wrote the new json file with the original key order (to have the nice git diff)
  1. what is the behavior of multiple keys
{
  "a": "A",
  "zfoo": "F1",
  "b": "B",
  "zfoo": "F2",
  "z": "Z"
}

will be

{
  "a": "A",
  "zfoo": "F2",
  "b": "B",
  "z": "Z"
}

or

{
  "a": "A",
  "b": "B",
  "zfoo": "F2",
  "z": "Z"
}
  1. how to change a value with or without updating the key order ?

If I want to keep the original order,
should I just make

j = decode( '{"a":"A","zfoo":"F1", "b":"B", "zfoo":"F2", "z":"Z"}' )
j.zfoo = "F3"
print(encode(j))

If I want to forget the original order for one key...
Is it possible to make :

j = decode( '{"a":"A","zfoo":"F1", "b":"B", "zfoo":"F2", "z":"Z"}' )
j.zfoo = nil -- one to remove the key (not catch by the __newindex handler)
j.zfoo = nil -- one to process the remove request by the __newindex handler
j.zfoo = "F3" -- now we can add the "zfoo" key again
print(encode(j))

to get {"a":"A", "b":"B", "z":"Z", "zfoo":"F3"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant