Skip to content

Instantly share code, notes, and snippets.

@wujiyu115
Created January 8, 2014 10:50
Show Gist options
  • Save wujiyu115/8314980 to your computer and use it in GitHub Desktop.
Save wujiyu115/8314980 to your computer and use it in GitHub Desktop.
lua:cocos2dxlua
function FormatSerialize(Object)
local function ConvSimpleType(v)
if type(v)=="string" then
return string.format("%q",v)
elseif type(v) =='number' or type(v) =='boolean' then
return tostring(v)
end
return string.format("%q",type(v))
end
local function RealFun(Object, Depth)
Depth = Depth or 0
Depth = Depth + 1
if Depth > 4 then
return '"null"'
end
if type(Object) == 'table' then
--if Object.__ClassType then return "<Object>" end
local Ret = {}
table.insert(Ret,'{\n')
for k, v in pairs(Object) do
--print ("serialize:", k, v)
local index = string.find(tostring(k),"__")
if index==nil then
local _k = ConvSimpleType(k)
if _k == nil then
error("key type error: "..type(k))
end
table.insert(Ret, _k )
table.insert(Ret,':')
table.insert(Ret,RealFun(v, Depth))
table.insert(Ret,',\n')
end
end
table.insert(Ret,'}\n')
return table.concat(Ret)
else
return ConvSimpleType(Object)
end
end
return RealFun(Object)
end
local function main()
-- run
local G = _G
_G._G = nil
local strBuf = FormatSerialize(G)
local file = io.open("cocos2dx_lua.json", "w+")
file:write(strBuf)
file:close()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment