Nerd wannabe

lazy weblog

Auto-validating JSON with optional documentation

leave a comment »

The idea is to have one of the nodes documenting the structure of the rest of them and to have the ability to write textual documentation for the values.

The node "schema?" is used to introduce this structure as a top-level node. The contents of the schema have keys in the form "key? The documentation is here", where the ? sign plays the role of a separator but also says something about the requirement for a key or value’s presence.

  • key! => key must be present
  • key? => key is optional
  • key+ => value must be non-null, and in the case of arrays it must have a length>0
  • key* => value may be null

"key", "key!" and "key+" are shorthand for "key!+",
"key?" and "key*" are shorthand for "key?*",

Any value is accepted and documents the type of the value: "", 0, 0.0, true, [], {} etc.

  • "" => any string
  • integer value = 0, value > 0 or value < 0 => integer number, positive only, negative only
  • double value = 0.0, value > 0.0 or value < 0.0 => double number, positive only, negative only
  • boolean value = true or false => boolean
  • array value = [], array value = [value1, value2..] => any array, or array containing elements in the form of either value1 or value2 (nested specification)
  • object value = {}, object value = { "key" : value, ... } => any object, or object with given schema (nested specification)

No constraints can be set on the order of elements.

Example:

{
  "schema?": {
    "title! This is shown in the window's titlebar" : "", 
    "count! The number of things" : 1, 
    "price! The price in USD" : 1.0,
    "head? Optional tag with metadata" : {
      "script? One or more script elements" : [{
        "source! Each one with a source string" : ""
      }], 
      "meta!  Array of metadata strings with at least one element" : [""], 
      "style!* Any array, even empty one"  : [], 
      "what?  Optional, any array"  : []  
    },
    "body! Main content" : {
      "div? Any number of divs": [],
      "span?": []
    }
  },

  "count" : 5,
  "title" : "JSON",
  "price" : 4.5,
  "head" : {
    "meta" : ["data"],
    "style" : [],
    "script" : [{ 
        "source" : "http://...js"
      }, { 
        "source" : "spdy://...dart", 
        "type" : "text/dart" 
      }]
  },
  "body": {
    "div" : []
  }
}

 

This also has the advantage of being valid JSON, after all. It can be a separate document used to validate objects passed over the wire, or included inline in e.g. a configuration file. And even if a JSON parser is not validating against the "schema?" the rest of the data is still usable.

Plus, you get comments in configuration files (because JSON does not allow comments..).

Written by vlad

July 4, 2014 at 8:03 am

Posted in ideas

Tagged with , ,

Static analysis screenshot

leave a comment »

Apple seems to make good GUIs and recently it makes compilers too, and IDEs of course.

Actually I don’t have a mac, never saw XCode, and never used CLang (yet).

But this makes me want them all:

static-analyzer

Remember, this is primary a C compiler! I saw few static analysis on C.

And then… just look, it draws a diagram. Mmm… arrows!

Good work!

Written by vlad

September 1, 2009 at 2:21 pm

Posted in ideas

Old: RAII Obliterator Pattern

leave a comment »

I implemnted an obliterator for C#. It works around this problem:


foreach (var item in list)
 if (item == "two")
   list.Remove(item);

System.InvalidOperationException: Collection was modified;enumeration operation may not execute.

The new version of the code is one line longer:


using (var obliterator = Obliterator.From(list))
  foreach (var item in list)
    if (item == "two")
      obliterator.Remove(item);

The Obliterator class :


/* This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the Do What The Fuck You Want
 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://sam.zoy.org/wtfpl/COPYING for more details. */ 

using System;
using System.Collections.Generic;

namespace Obliterator
{
 public class Obliterator<T> : IDisposable
 {
   ICollection<T> collection;
   List<T> toRemove;

   public Obliterator(ICollection<T> collection)
   {
     this.collection = collection;
     toRemove = new List<T>();
   }

   public void Remove (T item)
   {
     toRemove.Add(item);
   }

   public void Dispose()
   {
     foreach (var item in toRemove) collection.Remove(item);
   }

 }

 public static class Obliterator
 {
   public static Obliterator<T> From <T> (ICollection<T> collection)
   {
     return new Obliterator<T> (collection);
   }
 }
}

Written by vlad

August 22, 2009 at 2:13 pm

Posted in laugh

Gnome/Ubuntu NIH syndrome?

with 2 comments

There are at least 3 times when Ubuntu/Gnome teams* bit my user experience by:

- pushing alpha-quality technology

- taking a year to deliver a stable one

- just so that it can match a previous offer.

First: Totem

When I first tried it, it didn’t manage to play anything. When it did, it didn’t had subtitles. Or sound. Or it crashed. Or it crashed Nautilus. Or the browser.
However the Gnome Desktop kept on shipping it, even if 90% of people were using mplayer or xine or vlc. It doesn’t even matches these today, but I can call it stable. Why did they spent that much resources when they could have taken an existing, proven, technology?

Second: Network Manager

Ok, this is sad. Network manager started as an wifi deamon with a usermode icon panel. It didn’t even “speak” wpa_supplicant. Because of the deamon you cannot use wifi-radar an other decent solution. Even hamachi choked with it, because every new connection it sees, it tries to manage.

After one year of NM 0.6, the next version seems a little bit better. It even knows ppp and stuff. But the year with the network-manager 0.6 deamon, with prays for connections, I will never forget..:(

Third: Pulseaudio

First, I don’t even understand what’s that. From my point of view, it’s the thing that stops Audacity from working. And you cannot disable it (I mean, it was hard to find out it existed..)

Basically it locks your soundcard so that anybody should play through pulseaudio. Why the hell would something like that be pushed, along with programs known not to work with it??

I am beginning the wait for another year for a mature pulseaudio or a Gnome/Ubuntu Audacity replacement. “Not invented here” its the oposite of collaboration and hurts other OSs projects.

All these (3) technologies exist because of the Gnome desktop and the way it pushed them. With that “one-year-to-get-decent” policy, they would have failed in infancy.

Is this a bad thing?

update 05/07/2009: i reinstalled PulseAudio while trying to get my bluetooth headset to work (it didn’t). And now I am exposed to a ‘new’ bug, from 2008: I only hear static.

*Ubuntu/Gnome teams: what’s the relation between them?

Written by vlad

March 16, 2009 at 8:33 pm

Posted in morons

XAML vs MXML (part 1)

with 2 comments

Lately I have accepted a job involving WPF (Windows Presentation Foundation) and all the .net 3.5 nastiness.

I have a little previous exposure on Flex (MXML) and that only kick-started me to write a(nother) markup language for Gtk+.

It is only now that I came to reckon what XAML is about. And thought you might want to know how it stacks agains Flex, too.

Before going to code and charts, let me tell you that Microsoft’s offer is far more spreaded that Adobe’s:

Abobe Flex was just the flash plugin for “stage”-oriented advertising and online games, which later got widgets (the Flex SDK) then strong-typeness (ActionScript 3), and now tries to conquer the desktop world with Adobe AIR, which is a standalone runtime that gives access to local files.
The flash player is now a full-blown virtual machine, with a garbage collected, strong-typed language, with access to all the graphic features flash already has and since version 10, access to 3D features.

Microsoft has a very long history of dominating the desktop (Win32 hacks were common-knowledge for 15+ years), then switched to a “bytecode”-like technology which was running against the same Win32 platform widgets (.NET Windows.Forms).
Much more later they introduced this XAML markup (plus a widget toolkit refactoring – WPF) and implemented something for Firefox, something less for MacOS, helped some more the Novell Linux guys implementing even less on Linux (Moonlight)… and are generally heading towards RIA.
So now Microsoft offers full-blown desktop applications only for Windows, XBAP (WPF browser applications) for Firefox and IE but only on Windows, Silverlight applications on Windows and MacOS, and Moonlight on Linux (see above).
What is interesting is that if you compare the Silverlight 2 browser plugin vs a WPF application shows that the former cannot do networking using binary protocols, and cannot do accelerated 3D, both of which Flash/Flex do. So there should be no ‘browser plugin is limited’ mindset, but there is.

That is, the history of desktop-to-web transition is reversed for the two players (MS and Adobe).

(The good part from this race is that the old .res/.rc are ditched now, the macros from MFC are less and less used, and spaghetti code generated by Visual Studio.NET xxxx interface designers is now history (I am looking at you, Java!). Long live Application Markup!

Markup Comparison

So basically this is a comparison of the ‘markups‘ named used in Silverlight and WPF (XAML) and the one used in Flex and AIR (MXML), but also of a couple of other features of the platforms.

But how is this markup made? is it something like HTML DOM + JS? something like Mozilla’s XUL? Something like OpenLaszlo?

No. This is where MXML and XAML are alike: they are unlike any of the above.

What they do have in common is:

  • tags are (usually) classes
  • attributes are properties (fields with automatic getter and setter method call)
  • the root tag of the XML is the base class you’re inheriting
  • all other tags are instances that are added up to the parent container
  • both use xml namespaces in a similar way (although the URL schemes are different), to specify that the name of a class belongs to another AS3 package or C# namespace(s)

For example, this is the minimum source code for an application in Flex:
App.mxml

<mx:Application
  xmlns:mx="http://www.macromedia.com/2003/mxml"
  name="App"/>

And in WPF:
App.xaml

<Application
  x:Class="MyNamespace.App"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" />

One of the first differences is that XAML seems to need 2 xml prefixes: one for the Application and other tags, and a special one for meta-attributes specific to xaml. That, is ‘x:Class‘, is not an attribute of the Application class, but a meta-attribute valid only on the first tag.
The class name includes the namespace definition.

Flex, apparently, simply uses name, an existing attribute which is present on all UI components, to give the class name. But if you’re not stopping at generating classes with Flex Builder but code your own by hand, you’ll notice that in fact the file name is the name of the class, and the ‘name’ attribute is just run-time information (i.e. exactly the value of the ‘name’ property). Even more, the whole subdirectories path represents the package in which the class resides.

Including actual code

So far so good, you can define your application or another object like this. But how does one include ActionScript code (for MXML) or .NET code (for XAML) to.. actually accomplish something?

In MXML’s case, there is a ‘special’ tag in the mx namespace: <mx:Script> which accepts either direct text content (usually written as a CDATA section) or you can use <mx:Script source=… to specify a file from where the code should be included at compile time. Notice that Script is not actually an object that is added to the UI of the application, but a specially-handled tag.

<mx:Application
  xmlns:mx="http://www.macromedia.com/2003/mxml"
  name="App">
  <mx:Script>
    <![CDATA[
      //code here
    ]]>
  </mx:Script>
</mx:Application>

In XAML, the preffered way is code behind. You have your App.xaml file automatically associated with App.xaml.cs if the following apply: the .cs file (or .vb) defines the same class in the same namespace, the class in the .cs file is ‘partial’, and both are included in the files to be compiled list of Visual Studio. That is, automagically:)

public partial class App : Application
{
//code here
}

XAML’s x: prefix also includes an <x:Code> tag (notice that it’s in the XAML-specific namespace) but its use is highly discouraged by Microsoft.

--

This post was only a warm-up (for you, for me) to give the general impression of how the two technologies are similar a bit of their differences.

The part 2 will continue to explore all the features of these two Application Markups.

Written by vlad

December 6, 2008 at 11:21 pm

Posted in gtkaml

JavaFX: it’s JAVA!

with 3 comments

Edit (relevant quote):

When asked why developers and authors would want to choose JavaFX over other tools, such as Flex or Silverlight, Octavian summed it up in one word: Java

Before jumping there, remember: just because you can tell entire corporates that this is the ‘next best thing’, it doesn’t mean people will use it!

Just think ActiveX and their banishment from the Internet..,or, wait, you can think of the water applet!

Written by vlad

December 5, 2008 at 12:13 am

Posted in morons

Pointless update

with 5 comments

So.. I needed to re-cap a never-learned science, the computer one.

You see, when I joined a computer science university, I had no “official” CS background (math high-school, yadda-yadda).
I did had a computer at home in highschool – which allowed me to poke around with some ‘exotic’ VGA poking.. but never got into backtracking or other stuff my friends from CS highschool got.

Then, just when the 3rd year in university got to those subjects, I was _so_ employed I haven’t had the time to.. you know.. think before the exams.

So I bought some books now, that I am old enough to afford myself shopping from amazon (heh):
– the algorithm design manual – which I favoured over ‘CLR’ because everybody said CLR was ‘sooo basic’. Now I have to struggle with my english skills because the ADM is explained in plain english rather than scientific way
– the dragon book – yeah..
– learn to tango with D – the only D book available today (I love reading paper, you know..)
– erlang – the only book to – influenced by my reddit fellows

Soo.. I am planning re-writing gtkaml, the markup for UI that I ripped from my XAML/MXML/XUL experience.

But is it worth it?

I want to:
– stop using libxml2, switch to GMarkup, to minimize dependencies. But GMarkup doesn’t even discern CDATA from regular text (I need that for code in a gtkaml class)
– cleanly separate parsing from semantic analisys (lexing is XML).
– generate native Vala AST instead of text (I currently feed a generated source to the Vala parser)

Again.. why would I do it? It’s my first open-source attempt, I tried to announce it on vala list, I had as little as.. one user, then a couple of _month_ later tried to announce it on gnome-announce.

It seems like nobody’s getting where I’m going or doesn’t want to. Since I have this (mainly MXML) background, I concieved gtkaml as a shortcut for _me_ to learn the _Gtk_ UI widgets and their properties. I used it a couple of times to prototype UI for something that would have been to boring to write in C or in Vala.

I got at maximum 100 downloads per release, starring on gnomefiles and freshmeat. It lowers – the latest version, posted on gnome-announce, had less downloads.
Is it because, not having a well organized parser/code generator, every hacker scares away (from the error messages for example)??

I also get compared with glade/GtkBuilder that often that I explained in a previous post the differences. But still nobody cared.

Can somebody point me their open-source experience? is this normal? (i.e. to have an unique itch and scratch it?)

Written by vlad

August 21, 2008 at 8:00 pm

Posted in gtkaml, ideas, morons

Follow

Get every new post delivered to your Inbox.