Skip to content

Commit

Permalink
initial version of ObjectTemplate for easily transforming JSON to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Van Horn committed Dec 13, 2011
0 parents commit 6eeabdd
Show file tree
Hide file tree
Showing 6 changed files with 249 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>json2json</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>
18 changes: 18 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Copyright 2011 Joel Van Horn. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This is a little utility that will transform one JSON object structure to another structure defined by a template.

Created by Joel Van Horn. Free to use however you please.
193 changes: 193 additions & 0 deletions lib/ObjectTemplate.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@

sysmo = require 'sysmo'

class ObjectTemplate
constructor: (config, parent) ->
# if there is no node path, set to current node
config.path or= '.'
# ensure 'as' template exists
config.as or= {}
# convert property name to array
config.choose = [config.choose] if sysmo.isString config.choose

@config = config
@parent = parent

transform: (data) =>
@data = data;
node = @nodeToProcess data

return data if !node?

@processProperties node

processProperties: (node) =>
switch sysmo.type node
when 'Array' then @processArray node
when 'Object' then @processMap node
else node

# assume each array element is a map
processArray: (node) =>
# convert array to hash
if @config.key
context = {}

if @config.deep
binder = (element, index) =>
key = @chooseKey element
value = @processProperties element
context[key] = value
else
binder = (element, index) =>
key = @chooseKey element
value = @chooseValue(element, {})
formatted = @formatNodes node, value, key
context[formatted.key] = formatted.value
else
context = []
binder = (element, index) =>
value = @chooseValue(element, {})
formatted = @formatNodes node, value, index
context.push formatted.value

for element, index in node
binder element, index if @config.deep || @chooseNodes node, element, index
context

processMap: (node) =>
if @config.nest
context = {}
# loop through properties to pick up any key/values that should be nested
for key, value of node
if @chooseNodes node, value, key
nested_value = @chooseValue @getNode(node, key), {}
formatted = @nestNodes node, nested_value, key
context[formatted.key] = formatted.value
else
value = @chooseValue node, {}
context = value
context

nestNodes: (node, value, key) =>
return { key: key, value: value } if !sysmo.isFunction @config.nest
nested = @config.nest node, value, key
nested.key = key if 'key' not of nested
nested.value = value if 'value' not of nested
nested

formatNodes: () =>
# set default formatter or proxy existing
return { key: key, value: value } if !@config.format

formatted = @config.format node, value, key
formatted.key = key if 'key' not of formatted
formatted.value = value if 'value' not of formatted
formatted

chooseNodes: (node, value, key) =>
# convert array to chooser function that compares key names
if !@config.choose
return true if @config.all or (@config.key and @config.value)
@config.choose = []
for key, value of @config.as
@config.choose.push value.split('.')[0] if sysmo.isString(value)
# create callback for arry
if sysmo.isArray @config.choose
for element in @config.choose
return true if sysmo.isNumber(key) or element is key
return false
# if not a function yet, treat as boolean value
if !sysmo.isFunction @config.choose
# if config.key and config.value exist, most likely want to map all
choosen = @config.all or (@config.key and @config.value)
!!@config.choose #boolean
else
!!@config.choose node, value, key

chooseKey: (node) =>
# if @config.value exists, use that property of the array element as the value
switch sysmo.type @config.key
# custom function chooses value based on element
when 'Function' then @config.value node
# path specifies value from element
#when 'String' then @getNode node, @config.key
else @getNode node, @config.key

chooseValue: (node, context) =>
# if @config.value exists, use that property of the array element as the value
switch sysmo.type @config.value
# custom function chooses value based on element
when 'Function' then @config.value node
# path specifies value from element
when 'String' then @getNode node, @config.value
# normal mapping ensues
else @processTemplate node, context, @config.as

processTemplate: (node, context, template = {}) =>
defaultFilter = (node, value) -> value
# loop through properties in template
for key, value of template
# process mapping instructions
switch sysmo.type value
# string should be the path to a property on the current node
when 'String' then filter = (node, path) => @getNode(node, path) or null
# array gets multiple property values
when 'Array' then filter = (node, paths) => @getNode(node, path) for path in paths
# function is a custom filter for the node
when 'Function' then filter = true
# object should be { path: 'path.to.node', defaultValue: ... }
# when 'Object' then filter = (node, value) -> @getNode(node, value.path) or value.defaultValue
when 'Object' then filter = (node, config) => new @constructor(config, @).transform node
else filter = defaultFilter

# if the template specifies a function, pass node and key, otherwise it's an internal filter
value = if filter is true then filter node, key else filter node, value
# format key and value
formatted = @config@formatNodes node, value, key
context[formatted.key] = formatted.value

if !@config.nest
# loop through properties iode to pick up any key/values that should be choose
for key, value of node
# skip if node property already used, the property was specified by the template, or it should not be choose
if @paths(node).indexOf(key) is -1 and key not in context and @chooseNodes node, value, key
formatted = @formatNodes node, value, key
context[formatted.key] = formatted.value

context

nodeToProcess: (node) =>
@getNode node, @config.path

getNode: (node, path) =>
return node if path is '.'
@paths node, path
sysmo.getDeepValue node, path

# track the first property in a path for each node through object tree
paths: (node, path) =>
path = path.split('.')[0] if path

if !@pathCache
@pathNodes = if @parent then @parent.pathNodes else (@pathNodes = [])
@pathCache = if @parent then @parent.pathCache else (@pathCache = [])

index = @pathNodes.indexOf node

if !path
return if index isnt -1 then @pathCache[index] else []

if index is -1
paths = []
@pathNodes.push node
@pathCache.push paths
else
paths = @pathCache[index]

paths.push(path) if path and paths.indexOf(path) == -1
paths


# register module
module.exports = ObjectTemplate
2 changes: 2 additions & 0 deletions lib/json2json.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

exports.ObjectTemplate = require './ObjectTemplate'
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{ "name" : "json2json"
, "description" : "A simple way to transform a JSON object to another JSON structure"
, "version" : "0.0.1"
, "author" : "Joel Van Horn <[email protected]>"
, "repository" :
{ "type" : "git"
, "url" : "git://github.com/joelvh/json2json.git"
}
, "bugs" : { "web" : "https://github.com/joelvh/json2json/issues" }
, "os" : [ "linux", "darwin", "freebsd" ]
, "directories" : { "lib" : "./lib/" }
, "main" : "./lib/json2json"
, "dependencies" :
{ "sysmo" : "https://github.com/joelvh/Sysmo.js/tarball/master"
}
, "engines" : { "node" : ">=0.1.97" }
, "licenses" :
[ { "type" : "MIT"
, "url" : "http://github.com/joelvh/json2json/raw/master/LICENSE"
}
]
}

0 comments on commit 6eeabdd

Please sign in to comment.