Bunny83 / SimpleJSON

A simple JSON parser in C#
MIT License
735 stars 294 forks source link

Reading a zero as a key on a JSONNode #34

Closed Shadowing1983 closed 3 years ago

Shadowing1983 commented 4 years ago

Say my JSON looks like 0:{0:{"forces":{"soldiers":2,"bombers":0,"fighters":0,"mother_ships":[],"flag_ships":[],"super_flag_ships":[]} After I convert it to JSONNode it will look like [[{"forces":{"soldiers":2,"bombers":0,"fighters":0,"mother_ships":[],"flag_ships":[],"super_flag_ships":[]}

It only does this to 0. So it treats 0 as empty I guess. if the key is 1 then it will convert the int key to a 1 string "1":{"1":{"forces":{"soldiers":2,"bombers":0,"fighters":0,"mother_ships":[],"flag_ships":[],"super_flag_ships":[]}

So idk may just be my bad understanding of something.

Bunny83 commented 4 years ago

Well you start with

Say my JSON looks like

and there's already your issue ^^. The text you have posted is not valid json. Please read the definition at json.org

First of all you start your text with a key value pair which makes no sense since those can only appear inside an object. Also your brackets do not have their closing counterpart. Even when we assume you only posted a middle snippet of your actual json text the key of a key value pair always has to be a quoted string. So using an unquoted number as key doesn't work.

All that aside my parser is quite forgiving for mistakes like that, however you should not rely on that. Even I haven't tested it the parser should treat a wrongly used integer inside an object just as any other unquoted key. That should make no difference if it's 0 or 1. Please provide an actual json text example that is not truncated

Shadowing1983 commented 4 years ago

Sorry about that man

Here is the PHP array

array(1) { [0]=> array(1) { [0]=> array(4) { ["forces"]=> array(6) { ["soldiers"]=> int(1) ["bombers"]=> int(0) ["fighters"]=> int(0) ["mother_ships"]=> array(0) { } ["flag_ships"]=> array(0) { } ["super_flag_ships"]=> array(0) { } } ["technology"]=> array(1) { ["advance"]=> array(5) { ["enriched_naqahdah"]=> int(2) ["ascendancy"]=> int(0) ["development"]=> int(10) ["barricade"]=> int(0) ["veiled"]=> int(4) } } ["data"]=> array(17) { ["operativeness"]=> float(0.94) ["race"]=> string(6) "goauld" ["personality"]=> string(10) "psychopath" ["player_stance"]=> string(8) "economic" ["status_level"]=> int(1) ["endurance"]=> int(1) ["phlogiston"]=> bool(false) ["name"]=> string(0) "" ["army_owner_id"]=> int(0) ["army_id"]=> int(1) ["address"]=> int(0) ["networth"]=> int(0) ["x"]=> int(0) ["y"]=> int(0) ["planet_name"]=> string(0) "" ["subtract_endurance"]=> int(0) ["operativeness_damage"]=> float(0.06) } ["battle_points"]=> array(2) { ["raw_attack_points"]=> float(120) ["total_attack_points"]=> float(123.6) } } } }

Here it is in JSON

[[{"forces":{"soldiers":1,"bombers":0,"fighters":0,"mother_ships":[],"flag_ships":[],"super_flag_ships":[]},"technology":{"advance":{"enriched_naqahdah":2,"ascendancy":0,"development":10,"barricade":0,"veiled":4}},"data":{"operativeness":0.94,"race":"goauld","personality":"psychopath","player_stance":"economic","status_level":1,"endurance":1,"phlogiston":false,"name":"","army_owner_id":0,"army_id":1,"address":0,"networth":0,"x":0,"y":0,"planet_name":"","subtract_endurance":0,"operativeness_damage":0.06},"battle_points":{"raw_attack_points":120,"total_attack_points":123.6}}]]

So turns out I was wrong. The zero is being removed during the conversion to JSON Here is is if its using 1 as key

array(1) { [1]=> array(1) { [1]=> array(4) { ["forces"]=> array(6) { ["soldiers"]=> int(2) ["bombers"]=> int(0) ["fighters"]=> int(0) ["mother_ships"]=> array(0) { } ["flag_ships"]=> array(0) { } ["super_flag_ships"]=> array(0) { } } ["technology"]=> array(1) { ["advance"]=> array(5) { ["enriched_naqahdah"]=> int(0) ["ascendancy"]=> int(0) ["development"]=> int(0) ["barricade"]=> int(0) ["knowledge"]=> int(0) } } ["data"]=> array(15) { ["endurance"]=> int(1) ["player_stance"]=> string(9) "no stance" ["race"]=> string(6) "goauld" ["personality"]=> string(9) "architect" ["ancient_battleship"]=> bool(false) ["name"]=> string(0) "" ["army_owner_id"]=> int(1) ["army_id"]=> int(2) ["address"]=> int(0) ["networth"]=> int(0) ["x"]=> int(0) ["y"]=> int(0) ["status_level"]=> int(0) ["planet_name"]=> string(0) "" ["subtract_endurance"]=> int(0) } ["battle_points"]=> array(2) { ["raw_attack_points"]=> float(240) ["total_attack_points"]=> float(240) } } } }

{"1":{"1":{"forces":{"soldiers":2,"bombers":0,"fighters":0,"mother_ships":[],"flag_ships":[],"super_flag_ships":[]},"technology":{"advance":{"enriched_naqahdah":0,"ascendancy":0,"development":0,"barricade":0,"knowledge":0}},"data":{"endurance":1,"player_stance":"no stance","race":"goauld","personality":"architect","ancient_battleship":false,"name":"","army_owner_id":1,"army_id":2,"address":0,"networth":0,"x":0,"y":0,"status_level":0,"planet_name":"","subtract_endurance":0},"battle_points":{"raw_attack_points":240,"total_attack_points":240}}}}

Bunny83 commented 3 years ago

So, not really an issue :)