[OT] Do you see a problem in this serialization format ?

Basile B. via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 6 19:11:55 PDT 2016


I had to make a custom format because

- XML is not readable (think by hand edition; setting file)
- JSON is not good to represent D data types (an object must be 
created for each data, currently i have a json writer I could 
post a sample if you don't get what i mean)

I will use it as for Object Pascal streaming, to store the 
properties of the visual objects. The serializer uses a custom 
RTTI system instead of traits code. A factory could be used to 
create sub objects.

============================= SAMPLE 
===============================

Object root = "TextEn"
{
	Alignment alignment = "none"
	float width = "200"
	float height = "75"
	float left = "10"
	float top = "180"
	bool visible = "true"
	bool focusable = "true"
	bool wantMouse = "true"
	string caption = "Lorem ipsum"
	Brush fill = "Brush"
	{
		FillKind fillKind = "uniform"
		uint color = "4294965965"
		GradientKind gradientKind = "horizontal"
		FillAdjustment fillAdjustment = "none"
		Gradient gradient = "Gradient"
		Bitmap bitmap = "Bitmap"
		{
			Stream data = "[]"
		}
	}
	Brush stroke = "Brush"
	{
		FillKind fillKind = "uniform"
		uint color = "4287245282"
		GradientKind gradientKind = "horizontal"
		FillAdjustment fillAdjustment = "none"
		Gradient gradient = "Gradient"
		Bitmap bitmap = "Bitmap"
		{
			Stream data = "[]"
		}
	}
	Pen pen = "Pen"
	{
		float width = "1"
		Dash dash = "none"
		Join join = "miter"
		LineCap cap = "butt"
	}
	Font font = "Font"
	{
		float size = "32"
		string name = "/usr/share/fonts/truetype/FreeMono.ttf"
	}
	Justify justify = "left"
}

====================================================================

The structure is simple, I'm not a language technician but the 
grammar would be:
({ } = " are unamed literals)

escapes
   \" \n
Type
    [\S]+
name
    [\S]+
value
    "[escapes ^"]+" opt ObjectEntry

Entry
     Type name = value

Entries
     Entry0
     Entry1
     ....
     EntryN

ObjectEntry
    { Entries }

sub Object declarations may look strange, eg

     Stuff stuff = "Stuff" {}

instead of

     Stuff stuff = {}

but actually it was easier to parse whithout special case. For 
example once I reach the terminal double quote I just have to 
read while white and to check the character. If it's an opened 
brace I know that I'll have to read a ObjectEntry, otherwise it's 
Entry.

So, what do you think, is there any design flaw ?


More information about the Digitalmars-d mailing list