|
| 30 Jan 2016 08:56 PM |
been working on a data module for my framework. you guys have seen it before. I just recently gave it a few upgrades and documented it. current features:
1. Encode/Decode several of the most used built ins json compatible with out data loss. - cannot encode/decode region3 as of now. 2. Functions to manually check if a value is any of the built in data types.
3. Get the data type of a value via testing. Relatively resource heavy.
4. Encode and decode tables of values into a json string automatically! - cannot encode/decode region3 as of now. - cannot encode/decode instances as of now. version 1.2-1.30.16 approx. I have a hard time keeping track, not like version matters. Anyways enough stalling here is the code: http://hastebin.com/kusaxijamu.pas
please leave suggestions of what I should add to this module. current list of possible future updates: - acsii dinosuar at the top of the code - give encodejson/decodejson the ability to work with instances, soon, real soon. - give encodejson/decodejson the ability to work with region3's, they're a pain. - anti-gravity function @cpmod - time machine, need more JIJGAWATTS
smite me down god |
|
|
| Report Abuse |
|
DrHaximus
|
  |
| Joined: 22 Nov 2011 |
| Total Posts: 8410 |
|
|
| 30 Jan 2016 09:02 PM |
| i just scrolled down the page, but it looks a little sphaggeti-code-ish |
|
|
| Report Abuse |
|
|
| 30 Jan 2016 09:03 PM |
yeah... sadly it is very 'if-y' lol. But it works and theres no other way so.
smite me down god |
|
|
| Report Abuse |
|
DrHaximus
|
  |
| Joined: 22 Nov 2011 |
| Total Posts: 8410 |
|
|
| 30 Jan 2016 09:06 PM |
| if you had a list of members of each class type you could automate the process, maybe get a JSON form of the docs for each type |
|
|
| Report Abuse |
|
rvox
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 5380 |
|
|
| 30 Jan 2016 09:07 PM |
| ha noob my cerealizer is beter |
|
|
| Report Abuse |
|
|
| 30 Jan 2016 09:07 PM |
D̲O̲N̲T̲ ̲R̲U̲I̲N̲ ̲T̲H̲I̲S̲ ̲F̲O̲R̲ ̲M̲E̲ ̲H̲A̲X̲
smite me down god |
|
|
| Report Abuse |
|
|
| 30 Jan 2016 09:08 PM |
rvox let me see cause that sounds cool
smite me down god |
|
|
| Report Abuse |
|
rvox
|
  |
| Joined: 18 Feb 2011 |
| Total Posts: 5380 |
|
| |
| |
|
| 30 Jan 2016 10:53 PM |
Updated version that accually works
local HttpService = game:GetService 'HttpService'
lib = {}
--- EncodeColor3, turns built-in color3 class into a JSON compatiable. --- @para Color, the color 3 data you want to convert. local function EncodeColor3(Color3) local PsuedoClass = { r = Color3.r, b = Color3.b, g = Color3.g } return PsuedoClass end lib.EncodeColor3 = EncodeColor3
--- DecodeColor3, turns a psuedo JSON encode Color3 class to a Color3. --- @para JSONColor3, the color3 psuedo class you want to decode. local function DecodeColor3(JSONColor3) local Class = Color3.new( JSONColor3.r, JSONColor3.b, JSONColor3.g ) return DecodeColor3 end lib.DecodeColor3 = DecodeColor3
--- EncodeBrickColor, turns built-in Brickcolor class into a JSON compatiable. --- @para BrickColor, turns a brick color into a psuedo brick color class. local function EncodeBrickColor(BrickColor) return BrickColor.Name end lib.EncodeBrickColor = EncodeBrickColor
--- DecodeBrickColor, turns a psuedo JSON encode Color3 class to a Color3. --- @para JSONBrickColor, the brick color name you want to decode. local function DecodeBrickColor(JSONBrickColor) return BrickColor.new(JSONBrickColor) end lib.DecodeBrickColor = DecodeBrickColor
--- EncodeBrickColor, turns built-in Brickcolor class into a JSON compatiable. --- @para Vector3, the vector3 you want to convert into a vec3 psuedo class. local function EncodeVector3(Vector3) local PsuedoClass = { x = Vector3.x, y = Vector3.y, z = Vector3.z } return PsuedoClass end lib.EncodeVector3 = EncodeVector3
--- EncodeVector2, turns a built in Vector2 into a json compatiable psuedo class. --- @para Vector2, vector2 you want to convert into a vec2 psuedo class. local function EncodeVector2(Vector2) local PsuedoClass = { x = Vector2.x, y = Vector2.y }
return PsuedoClass end lib.EncodeVector2 = EncodeVector2
--- DecodeVector2, turns a psuedo vec2 class in to a full built-in Vector2 --- @para PsuedoVector2, the psuedo class you want to decode. local function DecodeVector2(PsuedoVector2) return Vector2.new(PsuedoVector2.x, PsuedoVector2.y) end lib.DecodeVector2 = DecodeVector2
--- DecodeVector3, turns a vector3 psuedo class into a built-in Vector3 --- @para JSONVector3, the psuedo vector3 class you want to decode. local function DecodeVector3(JSONVector3) return Vector3.new(JSONVector3.x, JSONVector3.y, JSONVector3.z) end lib.DecodeVector3 = DecodeVector3
--- Explicit block for the retrieve type function and dependent tests. do local GetItems = Enum.KeyCode.GetEnumItems local TestEvent = game.ChildAdded.connect local TestJunk = function() end
local TestFrame = Instance.new 'ImageLabel' local TestComponent = Instance.new 'Part' --- Function used to set a property to a value of a instance. local function Set(Parent, Property, Value) Parent[Property] = Value end
--- Independent test for independent use, they are also used in GetType. --- ... if you know the type you're trying to find can only be a range of --- ... 3 or less it would probably be best to write a custom function to --- ... find the type of instance you're dealing with to save usage! local function IsInstance(Value) return pcall(game.GetService, game, Value) end lib.IsInstance = IsInstance
local function IsVector3(Value) return pcall(Set, TestComponent, 'Position', Value) end lib.IsVector3 = IsVector3
local function IsCFrame(Value) return pcall(Set, TestComponent, 'CFrame', Value) end lib.IsCFrame = IsCFrame
local function IsBrickColor(Value) return pcall(Set, TestComponent, 'BrickColor', Value) end lib.IsBrickColor = IsBrickColor
local function IsColor3(Value) return pcall(Set, TestFrame, 'BackgroundColor3', Value) end lib.IsColor3 = IsColor3
local function IsEvent(Value) return pcall(TestEvent, Instance, function() end) end lib.IsEvent = IsEvent
local function IsUDim2(Value) return pcall(Set, TestFrame, "Size", Value) end lib.IsUDim2 = IsUDim2
local function IsEnum(Value) return pcall(function(Value) assert(Value == Enum or GetItems(Value), '') end) end lib.IsEnum = IsEnum local function IsConnection(Value) return pcall(function() return assert(type(Value.disconnect) == 'function' and Value.connected ~= nil,'') end) end lib.IsConnection = IsConnection
local function IsUDim(Value) return pcall(function() return UDim.new(0, 0) + Value end) end lib.IsUDim = IsUDim
local function IsVector2(Value) return pcall(Set, TestFrame, "ImageRectOffset", Value) end lib.IsVector2 = IsVector2
local function IsRay(Value) return pcall(workspace.FindPartOnRay, workspace, Value) end lib.IsRay = IsRay
local function IsRegion3(Value) return pcall(workspace.FindPartsInRegion3, workspace, Value) end lib.IsRegion3 = IsRegion3
--- RetrieveType, using a brute force method this function test each possible --- ... built-in datatypes to find Instance's data type. --- @para Instance, the instance you're want to know the type of. local function RetrieveType(Instance) local Type = type(Instance)
if Type ~= 'userdata' then print'd' return Type end
if IsInstance(Instance) then Type = 'Instance' print'd' elseif IsVector3(Instance) then Type = 'Vector3' print'd' elseif IsCFrame(Instance) then Type = 'CFrame' elseif IsBrickColor(Instance) then Type = 'BrickColor' elseif IsColor3(Instance) then Type = 'Color3' elseif IsEnum(Instance) then Type = 'Enum' elseif IsEvent(Instance) then Type = 'Event' elseif IsUDim2(Instance) then Type = 'UDim2' elseif IsConnection(Instance) then Type = 'Connection' elseif IsUDim(Instance) then Type = 'UDim' elseif IsVector2(Instance) then Type = 'Vector2' elseif IsRay(Instance) then Type = 'Ray' elseif IsRegion3(Instance) then Type = 'Region3' end
return Type end lib.RetrieveType = RetrieveType lib.retrieveType = RetrieveType end
--- Explicit block for json encoding/decoding functions. do --- This function is just used to attach an identifier to psuedo classes. local function EncodeEntity(Type, Value) return {Id = Type, Val = Value} end
--- EncodeJSON, converts defualt lua data types as well roblox built in types. --- @para Table, the table to encode into JSON format. local function EncodeJSON(Table) local ConvertedData = {} for Index, Entry in next, Table do local Type = lib.GetType(Entry) local EncodedEntry
if Type == 'Instance' then warn('Type of instance is not currently supported, this data will be lost.') elseif Type == 'Vector2' then EncodedEntry = EncodeVector2(Entry) elseif Type == 'Color3' then EncodedEntry = EncodeColor3(Entry) elseif Type == 'Vector3' then EncodedEntry = EncodeVector3(Entry) elseif Type == 'BrickColor' then EncodedEntry = EncodeBrickColor(Entry) elseif Type == 'CFrame' then EncodedEntry = EncodeVector3(Entry) elseif Type == 'table' then ConvertedData[Index] = EncodeJSON(Entry) else ConvertedData[Index] = Entry end
if EncodedEntry then ConvertedData[Index] = EncodeEntity(Type, EncodedEntry) end end return HttpService:JSONEncode(ConvertedData) end lib.EncodeJSON = EncodeJSON
--- DecodeJSON, decodes a json with encoded psuedo classes --- @para string, the json string coding. local function DecodeJSON(String) local BasicDecode = HttpService:JSONDecode(String) local FullDecode = {} for Index, Entry in next, BasicDecode do if type(Entry) == 'table' and DataTypes.Roblox['_' .. Entry.Id] then local Id = tostring(Entry.Id) local Value = Entry.Val local DecodedData if Id == 'Color3' then DecodedData = DecodeColor3(Value) elseif Id == 'Vector3' then DecodedData = DecodeVector3(Value) elseif Id == 'BrickColor' then DecodedData = DecodeBrickColor(Value) elseif Id == 'CFrame' then DecodedData = DecodeVector3(Value) elseif Id == 'Vector2' then DecodedData = DecodeVector2(Value) else DecodedData = Entry end FullDecode[Index] = DecodedData else FullDecode[Index] = Entry end end return FullDecode end lib.DecodeJSON = DecodeJSON end
return lib
smite me down god |
|
|
| Report Abuse |
|