Z425

Saturday, May 30, 2009

JSON with inheritence

Many games use some sort of inheritance for some of their content. This is basically a json preprocessor that supports inheritance using a couple of specific file location rules.

The path rules are pretty simple.

./ prefix for file scope ( internal to file )
../ prefix for file scope ( external to file )
/ prefix for global root scope
no prefix for local root scope ( traverse up to find a local root )

Path's are not absolute file system paths. So global does not literally mean the global root, just whatever is registered as a global or local. You'd never path something as c:\ or anything.

The actual inheritance is done via splitting a json name into two parts with a colon.

"goat:Animal":{ }

goat is the name. Animal is what it inherits from. The preprocessor doesn't really know or care what it means. It is just text.

To actually have the preprocessor do something you need to inherit something it knows about and the only thing that is is other .json files accessed via a path.

"goat:../Animal.json":{ }

goat is the name which inherits from the animal.json file that resides in the same directory as this file.

"goat:scripts/types/Animal.json":{ }

That'll inherit from the local root scope

"goat:/otherproject/types/Animal.json":{ }

That'll inherit from a different local root scope called otherproject.

"goat:../Animal.json/Goat":{ }

You can also go inside another file. This'll inherit Goat from inside the Animal.json file located in the same directory. Global and local root scope would also work.

"goat:./Goat":{ }

You can also locate within the same file. The parser is two pass so you can inherit from "objects" below the current object.

"goat:./something/else/Goat"

You can also go however deep you want into the file's json path too.

At this point I've not decided if I want to support arbitrary backtracking in paths ( dotdots. )

0 Comments:

Post a Comment



<< Home