Table wont serialize to JSON

Report any Hollywood bugs here
Post Reply
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Table wont serialize to JSON

Post by Bugala »

Is this a bug or do I use it wrong:

Code: Select all

OpenFile(1, "files/coingeckocoinprices.json")
hwtable = ReadTable(1)
CloseFile(1)

ForEach(hwtable, Function(id, content)
	DebugPrint(id)
	ForEach(content, Function(id, content2)
		DebugPrint(id)
		ForEach(content2, Function(id, content3)
			DebugPrint("id:"..id.." content3:"..content3)
		EndFunction)
	EndFunction)
EndFunction)	

json$ = SerializeTable(hwtable)
result:

Code: Select all

And Action!
telos
01-May-2022 00:00:00
id:time content3:01-May-2022 00:00:00
id:eur content3:0.0025100904434144
id:usd content3:0.0026465631171114
id:btc content3:7e-008
02-May-2022 00:00:00
id:time content3:02-May-2022 00:00:00
id:eur content3:0.0033683162036005
id:usd content3:0.0035501476667986
id:btc content3:9.2181930461747e-008
xbtx
01-May-2022 00:00:00
id:time content3:01-May-2022 00:00:00
id:eur content3:0.0081785900704152
id:usd content3:0.0086232569376628
id:btc content3:2.2829926340689e-007
02-May-2022 00:00:00
id:time content3:02-May-2022 00:00:00
id:eur content3:0.010236051524065
id:usd content3:0.010790693152082
id:btc content3:2.8046843498103e-007
Error in line 15 (testi.hws): Error serializing item!
I am sending that table for you through email Andreas.

Edit: Hollywood 9.1 in use.
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Table wont serialize to JSON

Post by airsoftsoftwair »

Bugala wrote: Thu Mar 30, 2023 12:33 pm I am sending that table for you through email Andreas.
Didn't get any email about this...
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Table wont serialize to JSON

Post by Bugala »

Sent on march 30 according to gmail. Is "andreas@airsoftsoftwair.de" not right?
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Table wont serialize to JSON

Post by airsoftsoftwair »

Oops, found your mail now, sorry. Your code doesn't work with Hollywood 9 because the JSON serializer in Hollywood 9 only supports a single serialization mode. Hollywood 10 features several extensions here so that arbitrary JSONs can be (de)serialized. I can make your code work by using Hollywood 10's new #SERIALIZEMODE_NAMED serializer by calling SetSerializeMode() like this:

Code: Select all

SetSerializeMode(#SERIALIZEMODE_NAMED)
json$ = SerializeTable(hwtable)
StringToFile(json$, "foo.json")
This code yields the following JSON which should be correct:

Code: Select all

{
	"telos": {
		"01-May-2022 00:00:00": {
			"time": "01-May-2022 00:00:00",
			"eur": 0.0025100904434144419,
			"usd": 0.0026465631171114102,
			"btc": 7.0000000000000005e-008
		},
		"02-May-2022 00:00:00": {
			"time": "02-May-2022 00:00:00",
			"eur": 0.003368316203600512,
			"usd": 0.003550147666798599,
			"btc": 9.218193046174652e-008
		}
	},
	"xbtx": {
		"01-May-2022 00:00:00": {
			"time": "01-May-2022 00:00:00",
			"eur": 0.0081785900704152391,
			"usd": 0.0086232569376627679,
			"btc": 2.2829926340688636e-007
		},
		"02-May-2022 00:00:00": {
			"time": "02-May-2022 00:00:00",
			"eur": 0.010236051524064795,
			"usd": 0.01079069315208179,
			"btc": 2.8046843498102605e-007
		}
	}
}
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Table wont serialize to JSON

Post by Bugala »

Thanks. Will change that code later to JSON version when I update to 10 first, will stick to fully Hollywood table for now. Just been planning to start using JSON to have more widely used standard.
Post Reply