[PATCH 2/2] Generate correct json

Salvo Tomaselli tiposchi at tiscali.it
Tue Aug 26 14:08:56 PDT 2014


First, let me point out that the json parser of Python, rejecting because of 
the extra commas is not wrong. That's how the format is defined.

I had started to fix the generation in a trivial way by changing all the bits 
where it puts commas (I wasn't using a do..while so it didn't look as good as 
the one Linus posted) but I got stuck in parts where there are functions 
appending strings like "{stuff}," which would also need to be fixed.

It can certainly be fixed but it requires more work.

The "fixing" is done in two stages but could be changed to do the moving and 
the scanning together, but I believe the code would be more complicated by 
mixing scanning and moving together.
Overall, the first step is O(n) and uses constant memory, while the 2nd step 
depends on the amount of extra commas and their relative distance, but there 
should be a somewhat linear correlation between that and the size of the 

Feel free to reject this if you think that it has to be done with a more 
proper solution.


In data martedì 26 agosto 2014 11:08:39, Dirk Hohndel ha scritto:
> On Tue, Aug 26, 2014 at 07:56:20PM +0200, Salvo 'LtWorf' Tomaselli wrote:
> > Not quite the solution I'd want, but the json generation would need
> > to be re-done from scratch to generate valid json code.
> ... and that is considered not worth it?

> > Instead, I wrote this, to find and remove the guilty commas. It tries to
> > move as little bytes around as possible, and writing it felt like
> > interviewing for a position at Facebook.
> I don't know about Facebook's interview process... but I don't think this
> is the way I would have done this. Why not just do the memmove on the fly
> while you scan the string?
> This seems rather fragile to me.

Salvo Tomaselli

"Io non mi sento obbligato a credere che lo stesso Dio che ci ha dotato di
senso, ragione ed intelletto intendesse che noi ne facessimo a meno."
                -- Galileo Galilei


More information about the subsurface mailing list