0

I'm looking for a tool, preferrably with a FLOSS license, which can do something like the following:

thetool --template mytemplate.c.someformat --data mydata.json --schema myschema.json --out myoutput.c

where thetool is the executable tool I'm looking for, mytemplate.c.someformat is my template file looking like a C file but with template style markup for data replacement, mydata.json contains data for populating the template, myschema.json is a schema explaining connections between data elements (nice to have, but not strictly mandatory) and myoutput.c is the generated C file.

Is there such a tool out there? Native json support is a key here, where error messages can highlight problematic areas in the json source. Xml is not an option. It does not need to have explicit support for C. It must be able to generate files in generic ASCII/UTF-8 format.

It must be able to run as a command line tool on both Windows and GNU/Linux. Also good if it can be linked as a library from python or C.

Example

syntax has not been verified, as I need the tool to do it.

mytemplate.c.someformat

#include <stdio.h>

int main(void) {
  #{{ for person in persons }}
    #{{ if person.likes.length > 0 }}
      printf("#{{ person.name }} likes:\n");
      #{{ for liked in person.likes }}
        printf("  * #{{ likes.name }}\n");
      #{{ endfor }}
    #{{ else }}
      printf("#{{ person.name }} doesn't like anyone.");
    #{{ endif }}
    printf("\n");
  #{{ endfor }}

  return(0);
}

mydata.json

{
  "persons": {
    {
      "id": "10",
      "name": "Mr Mister",
      "likes": {
        "20",
        "22"
      }
    },
    {
      "id": "20",
      "name": "Ms Missy",
      "likes": {
        "10",
      }
    },
    {
      "id": "22",
      "name": "No Body",
      "likes":
      {}
    }
  },
  "schema": "personSchema"
}

myschema.json

{
  "personSchema": {
    {
      "id": {
        "type": "integer",
        "attrib": "unique"
      }
      "name": {
        "type": "string"
      }
      "likes":
      {
        "type": "integer",
        "connection": "personSchema.id"
      }
    }
  }
}

myoutput.c

#include <stdio.h>

int main(void) {
      printf("Mr Mister likes:\n");
        printf("  * Ms Missy\n");
        printf("  * No Body\n");
    printf("\n");
      printf("Ms Missy likes:\n");
        printf("  * Mr Mister\n");
    printf("\n");
      printf("No Body doesn't like anyone.");
    printf("\n");

    return(0);
}

migrated from superuser.com Oct 21 '16 at 12:42

This question came from our site for computer enthusiasts and power users.

  • Why would you want to do this? If you generate many nearly-identical source files this shouts for refactoring your source ;-) I smell an X-Y problem here. – Jan Doggen Oct 21 '16 at 13:23
  • @JanDoggen: Which source files are near-identical? My example is of course minimized. In real world I have thousands of json entries. – Johan Oct 21 '16 at 13:28
  • does this work for your use case? github.com/mickep76/tf – Mike May 18 '17 at 16:55
1

There are many programming languages, many template engines, and many JSON parsers (and your request seems to be in a programming context), so just combine one of each and you'll get what you want.

For example, mustache is a simple template format with implementations in many various languages, including a Python pystache module. (It is itself based on CTemplate which comes as a C++ library.)

Super User isn't a script-writing service, but here's something to get started.

  • Thanks! I've added an example in the original post. Mustache seems a little too simple. I think I need more flexible expressions. Otherwise it seems like a good start. – Johan Oct 21 '16 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.