twolfson / spritesheet-templates

Convert spritesheet data into CSS or CSS pre-processor data
The Unlicense
107 stars 48 forks source link

JSON "light" template? #17

Closed tleunen closed 11 years ago

tleunen commented 11 years ago

Hi,

I'm thinking about creating a new JSON template, but without all the duplicates values for each sprite. What do you think of this?

{
    "image": "nested/dir/spritesheet.png",
    "w": 80,
    "h": 100
    "sprites": {
        "sprite1": {
            "x": 0,
            "y": 0,
            "w": 10,
            "h": 20
        },
        "sprite2": {
            "x": 0,
            "y": 0,
            "w": 10,
            "h": 20
        }
    }
}

And also, what do you think of removing the "spaces" param of the stringify function?

twolfson commented 11 years ago

Can you explain the use case for this?

As for stringify, I would rather make it possible to have a default of 4 but be able to override (e.g. null for no spaces)

twolfson commented 11 years ago

In fact, it might we could probably allow for custom iterator for JSON.stringify via cssOpts which would allow you to strip down the JSON to your liking.

twolfson commented 11 years ago

but still please explain your use case =)

tleunen commented 11 years ago

I don't really have a usecase. I use the JSON format when I need to dynamically download some assets and display them in an application. But with the current format, there are a lot of "duplicates" values, which cause the filesize to be bigger. If we can save a bit, it's always a good thing ;)

For the custom iterator, if you're talking about implementing a custom "toJSON", then yes it will work, and it's even a better solution as it will allow everyone to implement their own JSON without creating a new template.

twolfson commented 11 years ago

JSON.stringify accepts an iterator (replacer in the docs) for stringifying as its second parameter. That is what I was referring to.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

For your semi-use case, I would suggest composing the output from grunt-spritesmith via JSON grunt tasks:

https://npmjs.org/package/grunt-json-massager

https://npmjs.org/package/grunt-json-minify

https://npmjs.org/package/grunt-json-replace

http://gruntjs.com/plugins/json-

I am going to close this issue for now until it gains more critical mass from others.

tleunen commented 11 years ago

I think the replacer just allows you to modify a value for a certain property, not to completely rewrite the json.

But yep, I can read the output and modify it to be in the form in which I need, but this will add an additionnal step. I can also create a custom template and create the json I need before using stringify.