Jump to content
Sign in to follow this  
Mahdi Safsafi

Discussion: JSON and templates

Recommended Posts

Hello guys, I started this thread with hope to get advises and feedback.

I have a large complex data (splitted into fields and many of those fields share common properties) that must be saved on files. XML was the best choice but since I need to edit those files manually … it’s out of question to use XML (it just not user friendly).  Neither YAML was(I’m not a big fun of languages that use space and tab) ! So I found my self using JSON. But I got many issues : my files got very big(because there is no reference/template support) and not mentioning that I was desperately lacking for comments.

So I decided to write a custom language that inherits all JSON spec and provides additional supports for reference and templates.

Here is an example :

# templates:
# ---------
class TPerson<Name, Sex>{
  name: Name,
  sex : Sex,
}

class TMan<Name>  : TPerson<Name, "male">;
class TWoman<Name>: TPerson<Name, "female">;

class TChild<Name, Sex, Father, Mother> : TPerson<Name, Sex>{
  father: Father,
  mother: Mother,
}

class TBoy<Name, Father, Mother>  : TChild<Name, "male", Father, Mother>;
class TGirl<Name, Father, Mother> : TChild<Name, "female", Father, Mother>;

class TFamily<Name, Father, Mother, Childs>{
  name  : Name,
  father: Father,
  mother: Mother,
  childs: Childs,
}

# references:
# ----------
def mike : TMan<"mike">;
def mira : TWoman<"mira">;
def jaky : TBoy<"jaky", mike, mira>;
def lila : TGirl<"lila", mike, mira>;

# public (just like json):
[TFamily<"FamilyName", mike, mira, [jaky, lila]>]

After my language runs the above example, actually it’s able to generate a Perl object that supports reference so if I change the father name of the first child in run-time … the father’s name will change for all fields:

my $file   = 'test.xjson';
my $parser = XJSON::Parser->new();
my $xjson  = $parser->parse($file);
$xjson->[0]->{childs}->[0]->{father}->{name} = "NotMike"; # mike's name changed for all fields.

And converting it to JSON will dump the following:

[
   {
      "name" : "FamilyName",
      "father" : {
         "name" : "mike",
         "sex" : "male"
      },
      "childs" : [
         {
            "name" : "jaky",
            "sex" : "male",
            "mother" : {
               "sex" : "female",
               "name" : "mira"
            },
            "father" : {
               "name" : "mike",
               "sex" : "male"
            }
         },
         {
            "name" : "lila",
            "sex" : "female",
            "father" : {
               "name" : "mike",
               "sex" : "male"
            },
            "mother" : {
               "sex" : "female",
               "name" : "mira"
            }
         }
      ],
      "mother" : {
         "sex" : "female",
         "name" : "mira"
      }
   }
]

Right now, my language is draft and many things are implemented in a no proper way. I may rewrite it in C and consume it from Perl and probably Pascal.

So what do you think guys about the language ? and any idea about improving it ? and if you were at my situation what kind of solution you may think to use ?  

Share this post


Link to post

Maybe instead of thinking up a language, use TypeScript? In JS (and TS), JSON automatically becomes an object (Java Script Object Notation).

  • Like 1

Share this post


Link to post
32 minutes ago, Jacek Laskowski said:

Maybe instead of thinking up a language, use TypeScript? In JS (and TS), JSON automatically becomes an object (Java Script Object Notation).

Yeah that's good too ! and maybe I can use function just like template too.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×