Esempio n. 1
0
def generateSetterAndGetter(key, type, inden=0):
    assert isinstance(key, str)
    setFunctionName = 'set%s' % hump(key)
    getFunctionName = 'get%s' % hump(key)
    if type == 'boolean':
        if key[:2] != 'is':
            setFunctionName = 'set%s' % hump(key)
            getFunctionName = 'is%s' % hump(key)
        else:
            setFunctionName = 'set%s' % hump(key[2:])
            getFunctionName = key

    fun = '''
public void {0}({2} {1})$1
    this.{1} = {1};
$2

public {2} {3}()$1
    return this.{1};
$2
'''.format(setFunctionName, key, type,
           getFunctionName).replace('$1', '{').replace('$2', '}')
    ret = []
    for line in fun.split('\n'):
        ret.append('\t' * (inden) + line)
    return reduce(lambda x, y: '{}\n{}'.format(x, y), ret)
Esempio n. 2
0
def generateJavaClass(data, key='DEMO', inden=0):
    assert isinstance(data, dict)
    classInden = inden
    methodInden = inden + 1
    result = ''
    if inden == 0:
        result += '''
//package info here

import java.io.Serializable;
import java.util.List;

/**
* Created By JsonAll
* Copyright@Nexttec Inc, 2020.
*/

public class JsonData implements Serializable$0
    /**
    {}
    */
'''.format(data).replace('$0', '{')
    else:
        result += '\n\n'
        result += '\t' * classInden + 'public class {0} implements Serializable$0\n'.format(
            hump(key)).replace('$0', '{')
        result += '\t' * methodInden + '/**\n'
        result += '\t' * methodInden + '{}\n'.format(data)
        result += '\t' * methodInden + '*/\n\n'
    innerClass = {}
    setterAndGetters = []
    lastIndex = len(data.keys()) - 1
    keys = list(data.keys())
    keys.sort()
    for key in keys:
        value = data[key]
        result += '\t' * methodInden
        if isinstance(value, str):
            result += 'private String ' + key + ';'
            setterAndGetters.append(
                generateSetterAndGetter(key, "String", methodInden))
        elif isinstance(value, float):
            result += "private float " + key + ';'
            setterAndGetters.append(
                generateSetterAndGetter(key, "float", methodInden))
        elif isinstance(value, bool):
            result += "private boolean " + key + ';'
            setterAndGetters.append(
                generateSetterAndGetter(key, "boolean", methodInden))
        elif isinstance(value, int):
            result += "private int " + key + ';'
            setterAndGetters.append(
                generateSetterAndGetter(key, "int", methodInden))
        elif isinstance(value, bytes):
            result += "private byte[] " + key + ';'
            setterAndGetters.append(
                generateSetterAndGetter(key, "byte[]", methodInden))
        elif isinstance(value, dict):
            innerClass[key] = value
            result += "private %s " % hump(key) + key + ';'
            setterAndGetters.append(
                generateSetterAndGetter(key, hump(key), methodInden))
        elif isinstance(value, list):
            types = set()
            for item in value:
                types.add(type(item))
            if len(types) > 1:
                print(types)
                return key + "对应的数据类型不一致", 1
            if value:
                valueItem = value[0]
                if isinstance(valueItem, str):
                    result += "private String[] " + key + ';'
                    setterAndGetters.append(
                        generateSetterAndGetter(key, "String[]", methodInden))
                elif isinstance(valueItem, float):
                    result += "private float[]" + key + ';'
                    setterAndGetters.append(
                        generateSetterAndGetter(key, "float[]", methodInden))
                elif isinstance(valueItem, bool):
                    result += "private boolean[] " + key + ';'
                    setterAndGetters.append(
                        generateSetterAndGetter(key, "boolean[]", methodInden))
                elif isinstance(valueItem, int):
                    result += "private int[] " + key + ';'
                    setterAndGetters.append(
                        generateSetterAndGetter(key, "int[]", methodInden))
                elif isinstance(valueItem, bytes):
                    result += "private byte[] " + key + ';'
                    setterAndGetters.append(
                        generateSetterAndGetter(key, "byte[]", methodInden))
                elif isinstance(valueItem, dict):
                    standardValue = getStandardListItem(value)
                    innerClass[key] = standardValue
                    result += "private List<%s> " % hump(key) + key + ';'
                    setterAndGetters.append(
                        generateSetterAndGetter(key, "List<%s>" % hump(key),
                                                methodInden))
            else:
                result += 'private List<Object> ' + key + ';'
                setterAndGetters.append(
                    generateSetterAndGetter(key, "List<Object>", methodInden))
        result += '\n'

    if setterAndGetters:
        for item in setterAndGetters:
            result += item
        # result = result.strip()

    if innerClass:
        for k, v in innerClass.items():
            ret, err = generateJavaClass(v, k, inden + 1)
            if err == 0:
                result += ret

    result += '\n' + '\t' * classInden + '}'
    return result, 0
Esempio n. 3
0
def generateKotlinClass(data, key='DEMO', inden=0):
    assert isinstance(data, dict)
    classInden = inden
    methodInden = inden + 1
    result = ''
    if inden == 0:
        result += '''
//package info here

import java.io.Serializable
import java.util.List

/**
Created By JsonConverter
Copyright@Nexttec Inc, 2020.

{}
*/
data class DemoClass(
'''.format(data)
    else:
        result += '\n\n' + '/**\n'
        result += '{}\n'.format(data)
        result += '*/\n'
        result += 'data class {0}(\n'.format(hump(key))
    innerClass = {}
    setterAndGetters = []
    index = 0
    lastIndex = len(data.keys()) - 1
    keys = list(data.keys())
    keys.sort()
    for key in keys:
        value = data[key]
        result += '\t'
        if isinstance(value, str):
            result += 'val ' + key + ': String'
        elif isinstance(value, float):
            result += 'val ' + key + ': Float,'
        elif isinstance(value, bool):
            result += 'val ' + key + ': Boolean'
        elif isinstance(value, int):
            result += 'val ' + key + ': Int'
        elif isinstance(value, bytes):
            result += 'val ' + key + ': ByteArray'
        elif isinstance(value, dict):
            innerClass[key] = value
            result += 'val ' + key + ': ' + hump(key)
        elif isinstance(value, list):
            types = set()
            for item in value:
                types.add(type(item))
            if len(types) > 1:
                print(types)
                return key + "对应的数据类型不一致", 1
            if value:
                valueItem = value[0]
                if isinstance(valueItem, str):
                    result += 'val {}: List<{}>'.format(key, 'String')
                elif isinstance(valueItem, float):
                    result += 'val {}: List<{}>'.format(key, 'Float')
                elif isinstance(valueItem, bool):
                    result += 'val {}: List<{}>'.format(key, 'Boolean')
                elif isinstance(valueItem, int):
                    result += 'val {}: List<{}>'.format(key, 'Int')
                elif isinstance(valueItem, bytes):
                    result += 'val {}: List<{}>'.format(key, 'ByteArray')
                elif isinstance(valueItem, dict):
                    result += 'val {}: List<{}>'.format(key, hump(key))
                    standardValue = getStandardListItem(value)
                    innerClass[key] = standardValue
            else:
                result += 'val {}:List<Any>'.format(key)
        if index != lastIndex:
            result += ','
        index += 1
        result += '\n'
    result += ') : Serializable'

    if innerClass:
        for k, v in innerClass.items():
            if k in classNames:
                continue
            classNames.append(k)
            ret, err = generateKotlinClass(v, k, inden + 1)
            if err == 0:
                result += ret

    return result, 0
Esempio n. 4
0
def generateGoStruct(data, key='DEMO', inden=0):
    assert isinstance(data, dict)
    classInden = inden
    methodInden = inden + 1
    result = ''
    if inden == 0:
        result += '''
//package info here

/**
Created By JsonAll
Copyright@Nexttec Inc, 2020.

{}
*/
type JsonData struct$1
'''.format(data).replace('$1', '{')
    else:
        result += '\n\n' + '/**\n'
        result += '{}\n'.format(data)
        result += '*/\n'
        result += 'type {0} struct$1\n'.format(hump(key)).replace('$1', '{')
    innerClass = {}
    setterAndGetters = []
    index = 0
    lastIndex = len(data.keys()) - 1
    keys = list(data.keys())
    keys.sort()
    for key in keys:
        value = data[key]
        result += '\t'
        if isinstance(value, str):
            result += '{} string `json:"{}"`'.format(hump(key), key)
        elif isinstance(value, float):
            result += '{} float64 `json:"{}"`'.format(hump(key), key)
        elif isinstance(value, bool):
            result += '{} bool `json:"{}"`'.format(hump(key), key)
        elif isinstance(value, int):
            result += '{} int64 `json:"{}"`'.format(hump(key), key)
        elif isinstance(value, bytes):
            result += '{} []byte `json:"{}"`'.format(hump(key), key)
        elif isinstance(value, dict):
            innerClass[key] = value
            result += '{} {} `json:"{}"`'.format(hump(key), hump(key), key)
        elif isinstance(value, list):
            types = set()
            for item in value:
                types.add(type(item))
            if len(types) > 1:
                print(types)
                return key + "对应的数据类型不一致", 1
            if value:
                valueItem = value[0]
                if isinstance(valueItem, str):
                    result += '{} []string `json:"{}"`'.format(hump(key), key)
                elif isinstance(valueItem, float):
                    result += '{} []float64 `json:"{}"`'.format(hump(key), key)
                elif isinstance(valueItem, bool):
                    result += '{} []bool `json:"{}"`'.format(hump(key), key)
                elif isinstance(valueItem, int):
                    result += '{} []int64 `json:"{}"`'.format(hump(key), key)
                elif isinstance(valueItem, bytes):
                    result += '{} [][]byte `json:"{}"`'.format(hump(key), key)
                elif isinstance(valueItem, dict):
                    result += '{0} []{0} `json:"{1}"`'.format(hump(key), key)
                    standardValue = getStandardListItem(value)
                    innerClass[key] = standardValue
            else:
                result += '{0} []interface$0$1 `json:"{1}"`'.format(
                    hump(key), key).replace('$0', '{').replace('$1', '}')
        result += '\n'
    result += '}'

    if innerClass:
        for k, v in innerClass.items():
            if k in classNames:
                continue
            classNames.append(k)
            ret, err = generateGoStruct(v, k, inden + 1)
            if err == 0:
                result += ret

    return result, 0