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

Add support for tables #15

Open
NEO97online opened this issue Oct 15, 2023 · 2 comments
Open

Add support for tables #15

NEO97online opened this issue Oct 15, 2023 · 2 comments

Comments

@NEO97online
Copy link

Table from modules/Hash_Table.jai is empty when parsing typed JSON, it would be great to have support for loading arbitrary data as a Table.

@rluba
Copy link
Owner

rluba commented Oct 16, 2023

Can you give me a concrete example of what you want to parse into what? I want to make sure I understand your problem correctly.

@NEO97online
Copy link
Author

NEO97online commented Oct 17, 2023

@rluba sure thing.

Given this JSON:

{
	"name": "Consolas",
	"char_widths": {
		"!": 3,
		"I": 3
	}
}

I want to parse it into a struct like this:

Font :: struct {
    name: string;
    char_widths: Table(u8, int);
}

The way I'm handling it without first-class support for tables is by doing this:

FontJsonData :: struct {
    name: string;
    char_widths: JSON_Value;
}

And I load it as FontJsonData, then I transform it like this:

    if font_data.char_widths.type == .OBJECT {
    	for font_data.char_widths.object {
    		if it.type == .NUMBER {
    			table_add(*font.char_widths, it_index[0], cast(int) it.number);
    		} else {
    			print("[load_computer_font] char_widths fields should all be numbers!");
    			return false, .{};
    		}
    	}
    } else {
	print("[load_computer_font] char_widths should be an object!");
	return false, .{};
    }

So I would imagine a similar parsing code to populate a Table built into the library, similar to what is done for dynamic arrays.

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

2 participants