mercredi 7 décembre 2022

How to Generate C++ class/ methods from JSON data? [closed]

hmmm.Sorry but I think my question is very specific that I want to know how should I generate c++ code from json data..

I am getting a json response from one request ( the response ie the json will always contain accounts array with 0 to 25 elements)

for eg consider below response : res

res = {
 "accounts": [
        {
            "name": "CandyMachine",
            "type": {
                "kind": "struct",
                "fields": [
                    {
                        "name": "authority",
                        "type": "string"
                    },
                    {
                        "name": "wallet",
                        "type": "string"
                    },
                    {
                        "name": "tokenMint",
                        "type": "string"
                    },
                    {
                        "name": "itemsRedeemed",
                        "type": "u64"
                    },
                    {
                        "name": "data",
                        "type": "string"
                    }
                ]
            }
        },
        {
            "name": "CollectionPDA",
            "type": {
                "kind": "struct",
                "fields": [
                    {
                        "name": "mint",
                        "type": "string"
                    },
                    {
                        "name": "candyMachineNo",
                        "type": "u64"
                    }
                ]
            }
        },
        {
            "name": "FreezePDA",
            "type": {
                "kind": "struct",
                "fields": [
                    {
                        "name": "candyMachine",
                        "type": "string"
                    },
                    {
                        "name": "allowThaw",
                        "type": "bool"
                    },
                    {
                        "name": "frozenCount",
                        "type": "u64"
                    },
                    {
                        "name": "mintStart",
                        "type": "string"
                    },
                    {
                        "name": "freezeTime",
                        "type": "u64"
                    },
                    {
                        "name": "freezeFee",
                        "type": "u64"
                    }
                ]
            }
        }
    ]
}

Now according to above json response. I want to create class for each of the element of accounts array automatically

To do the above, I found this ts library through which I can generate typescript class by doing this way.

import { Project } from "ts-morph"

 const project = new Project()
 idl.accounts?.forEach((acc) => {
    const src = project.createSourceFile(
      outPath(`accounts/${acc.name}.ts`),
      "",
      {
        overwrite: true,
      }
   )
   const fields = acc.type.fields
   const name = acc.name

  const cls = src.addClass({
      isExported: true,
      name: name,
      properties: fields.map((field) => {
        return {
          isReadonly: true,
          name: field.name,
          type: field.type
        }
      }),
    })
}

which creates this typescript class

export class CandyMachine {
  readonly authority: string
  readonly wallet: string
  readonly tokenMint: string
  readonly itemsRedeemed: number
  readonly data: string
}

export class CollectionPDA {
  readonly mint : string
  readonly candyMachineNo: number
}

But I want to create similar class but in C++.

ie

 class CandyMachine
    { 
      std::string authority,
      std::string wallet,
      std::string tokenMint,
      uint64 itemsRedeemed,
      std::string data,
    }
    
    class CollectionPDA
    { 
      std::string mint,
      uint64 candyMachineNo,
    }

similarly for FreezePDA.

Can someone please tell me any way to achieve this ? How to generate C++ code ?

Aucun commentaire:

Enregistrer un commentaire