Last active
July 23, 2021 09:07
-
-
Save erezsh/ae3130b5f077f704a3983872b2f16e57 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let tree = parser.parse('{"c": ["a", "b",2]}') | |
// Object-based transformer | |
let OT = lark.Transformer.fromObj({ | |
array: (args) => args, | |
pair: (args) => args, | |
number: ([x]) => parseInt(x.value), | |
string: ([x]) => x.value.slice(1, -1), | |
object: (entries) => Object.fromEntries(entries) | |
}) | |
// Class-based transformer | |
class CT extends lark.Transformer { | |
array(args) { | |
return args | |
} | |
pair(args) { | |
return args | |
} | |
string([x]) { | |
return x.value.slice(1, -1) | |
} | |
number([x]) { | |
return parseInt(x.value) | |
} | |
object(entries) { | |
return Object.fromEntries(entries) | |
} | |
} | |
let json1 = OT.transform(tree) | |
let json2 = new CT().transform(tree) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment