KubeJS

KubeJS

69M Downloads

JsonIO does not support non-map top level objects

Dakedres opened this issue ยท 8 comments

commented

Minecraft Version

1.18.2

KubeJS Version

1802.5.4-build.500

Rhino Version

1802.1.14-build.182

Architectury Version

4.4.71

Forge/Fabric Version

40.1.51

Describe your issue

The following script does not behave as expected

JsonIO.write('test', 'hehe')

console.log('Tosti')
console.log(JsonIO.read('test') )

Which will write to the kubejs log:

[01:58:34] [INFO ] Tosti
[01:58:34] [INFO ] null

It is expected that "hehe" is written, rather than null. The test file indeed is created as expected in the instance root.

The log attached has some other scripts and mods running. This issue was also present in kubejs 1802.5.4-build.492

Crash report/logs

https://paste.ee/p/jGJul

commented

Just to be sure I followed the logic to make sure that it wasn't an issue of writing strings or something.
Looking at the JsonIO.write function:
public static void write(Path path, @Nullable JsonElement json) throws IOException
The second parameter needs to be a JsonElement.
And 'hehe' is a string.
This means a type wrapper is in charge of converting it to a JsonElement.
That type wrapper registered here is controlled by the function found here.
Following the logic of that method: a string does not match any of the conditions on lines 41 to 47 so it could be line 49 that is returning JsonNull.INSTANCE if it is returning null.
Which is the method JsonUtils.of, which is from rhino (which you can tell by this line here ).
And following JsonUtils.of we see that there is the handling of strings.
Which means that it's not a logical error with converting String to JsonElement but rather something to do with reading or writing to the actual file.
We know reading the file works, just not immediately after it is written to.
We know the file is written eventually.
My best guess is there is a delay between when something writes the file and then reads from it.
My best guess for possible issues that could cause that would be the fact that the file writer is never explicitly closed after use. Same thing here with the file reader.

commented

This happens whatever the json file contains, be it an array, and object or a string (if that even gets written properly)

commented

My best guess for possible issues that could cause that would be the fact that the file writer is never explicitly closed after use. Same thing here with the file reader.

@Hunter19823 That's not an issue, the writer is contained in a try-with-resources statement, meaning it is automatically closed and freed up at the end of the block. However, I don't doubt that the file isn't immediately fully flushed to the disk by the time write finishes, so the read call may be too early still

commented

Revisiting this issue with more sleep I've realised it also returns null with absolutely no writing beforehand.

console.log('Tosti')
console.log(JsonIO.read('test') )

Still just returns null. As always, the file contains "hehe"

commented

Yeah so... writing anything except a JSON object at the top level of the file means it's not going to work, it turns out. That part of the code still uses MapJS (which I was HOPING we could get rid of soon but nope...), so predictably, it expects a map (i.e. a json object at the top level)

commented

If you could confirm that writing a json object to a file also doesn't work, that'd be great, otherwise, I'd likely close this issue for now until we have some time to redo KubeJS' JSON system in general ^^

commented

I've tested wrapping it in a normal object and that works fine, thanks for the temp fix :)

commented

Going to add this to the 6.0 milestone for now, since Lat is looking to get rid of MapJS and ListJS entirely by then