Giter Site home page Giter Site logo

treedoc_ts's Introduction

GitHub Actions status codecov

Treedoc

Treedoc is an abstraction for tree-structured file formats such as JSON, YAML, and XML. It defines a simple object model similar to DOM for XML. But as an abstraction, the model itself is format agnostic. With this abstraction, it decouples the application from particular formats for configuration, persistent and communication serialization, etc. This allows the application to switch to different formats easily.

This library is an implementation of Treedoc with typescript. It also includes a parser for JSONex as a reference implementation for Treedoc model.

Background

Data serialization / de-serialization for configuration, persistent and communication are critical parts for almost any applications. The text-based serialization formats become more and more popular due to its simplicity. The formats have been evolved for many generations, from proprietary implementation to XML, YAML, and JSON, etc. Usually, the decision of which format is critical as once it's decided, the application will be coupled with the format. To switch to different format will be a huge refactor effort. However, the particular format shouldn't be the critical decision and the format will keep evolving. So it's important to decouple the application from particular formats. Treedoc is the solution for this decoupling.

JSONex

JSONex is a JSON extension format that makes JSON more friendly for configuration and persistent. This is a typescript port from Java implementation. Please refer to JSONex spec for more details.

Usage

Basic Usage

import { TDJSONParser, TDJSONParserOption, TDNodeType } from 'treedoc';

const o1 = TDJSONParser.get().parse(new TDJSONParserOption(jsonStr)).toObject();

Advanced Usage

Please refer to the test class TDJsonParser.spec.ts for more details.

Live Demo

http://treedoc.org

License

Copyright 2019-2022 Treedoc.org
Author/Developer: Jianwu Chen

Use of this source code is governed by an MIT-style license that can be found in the LICENSE file or at https://opensource.org/licenses/MIT.

treedoc_ts's People

Contributors

jianwu avatar

Stargazers

qǝʍᴉʍǝp avatar  avatar  avatar 湫曗 avatar Jarrod Roberson avatar Joe avatar

Watchers

 avatar James Cloos avatar

treedoc_ts's Issues

nested json point to the wrong parent

Hi,

I have a

this.rootFieldItem= new FieldItem( {fieldName:"Root", fieldType:FIELD_ITEM_TYPE.FIELD_GROUP_ROOT})
this.rootFieldItem.arrChildFieldItem=[
          new FieldItem({fieldName:"Field 1", fieldType:FIELD_ITEM_TYPE.FIELD_ITEM_LEAF, parent:this.rootFieldItem, fieldValue:"aa", fieldSize:5}),
          new FieldItem({fieldName:"Field 2", fieldType:FIELD_ITEM_TYPE.FIELD_ITEM_LEAF, parent:this.rootFieldItem, fieldValue:"bb", fieldSize:6}),
          new FieldItem({fieldName:"Group 1", fieldType:FIELD_ITEM_TYPE.FIELD_GROUP, parent:this.rootFieldItem}),
          new FieldItem({fieldName:"Field 3", fieldType:FIELD_ITEM_TYPE.FIELD_ITEM_LEAF, parent:this.rootFieldItem, fieldValue:"cc", fieldSize:10})
]  
this.rootFieldItem.arrChildFieldItem[2].arrChildFieldItem=[
          new FieldItem({ fieldName:"Field 4", fieldType:FIELD_ITEM_TYPE.FIELD_ITEM_LEAF, parent:this.rootFieldItem.arrChildFieldItem[2],fieldValue:"cc1", fieldSize:5}),
          new FieldItem({fieldName:"Field 5", fieldType:FIELD_ITEM_TYPE.FIELD_ITEM_LEAF, parent:this.rootFieldItem.arrChildFieldItem[2],fieldValue:"cc2f", fieldSize:5})
]

This is what i got after stringify using treenode

{"fieldId":"163566516048824","fieldName":"Root","fieldType":0,"fieldValue":"","fieldSize":-1,"arrChildFieldItem":  
 [{"fieldId":"163566516048937","fieldName":"Field 1","fieldType":2,"parent":{"$ref":"../../../"},"fieldValue":"aa","fieldSize":5}, 
 {"fieldId":"163566516048990","fieldName":"Field 2","fieldType":2,"parent":{"$ref":"../../../"},"fieldValue":"bb","fieldSize":6}, 
 {"fieldId":"163566516048972","fieldName":"Group 1","fieldType":1,"parent":{"$ref":"../../../"},"fieldValue":"","arrChildFieldItem": 
 [{"fieldId":"163566516049027","fieldName":"Field 4","fieldType":2,"parent":{"$ref":"../../../"},"fieldValue":"cc1","fieldSize":5}, 
 {"fieldId":"163566516049016","fieldName":"Field 5","fieldType":2,"parent":{"$ref":"../../../"},"fieldValue":"cc2f","fieldSize":5}]}, 
 {"fieldId":"163566516049072","fieldName":"Field 3","fieldType":2,"parent":{"$ref":"../../../"},"fieldValue":"cc","fieldSize":10}]}  

Notice that the the stringified records containing fieldName "Field 4" and "Field 5" , the parent attribute seem to be pointing incorrectly.
All the 7 records' parents attribute seem to be pointing to the same thing, when actually records containing field 4 and field 5 should be pointing to a different object for their parent attribute.

nested json point to the wrong parent

Hi,
With regards to my raised issue on #9, i think the issue not yet resolved.

To reiterate on what is raised on my previous issue:
All FieldItem objects ' parent are pointing "Root" fieldItem (which is correct), except for "Field 4" and "Field 5" fieldItem instances' whose parent also points to the "Root" (which is incorrect). The "Field 4" and "Field 5" should be pointing to "Group 1" fieldItem instead.

this.rootFieldItem= new FieldItem( {fieldName:"Root", fieldType:FIELD_ITEM_TYPE.FIELD_GROUP_ROOT})
this.rootFieldItem.arrChildFieldItem=[
          new FieldItem({fieldName:"Field 1", fieldType:FIELD_ITEM_TYPE.FIELD_ITEM_LEAF, parent:this.rootFieldItem, fieldValue:"aa", fieldSize:5}),
          new FieldItem({fieldName:"Field 2", fieldType:FIELD_ITEM_TYPE.FIELD_ITEM_LEAF, parent:this.rootFieldItem, fieldValue:"bb", fieldSize:6}),
          new FieldItem({fieldName:"Group 1", fieldType:FIELD_ITEM_TYPE.FIELD_GROUP, parent:this.rootFieldItem}),
          new FieldItem({fieldName:"Field 3", fieldType:FIELD_ITEM_TYPE.FIELD_ITEM_LEAF, parent:this.rootFieldItem, fieldValue:"cc", fieldSize:10})
]  
this.rootFieldItem.arrChildFieldItem[2].arrChildFieldItem=[
          new FieldItem({ fieldName:"Field 4", fieldType:FIELD_ITEM_TYPE.FIELD_ITEM_LEAF, parent:this.rootFieldItem.arrChildFieldItem[2],fieldValue:"cc1", fieldSize:5}),
          new FieldItem({fieldName:"Field 5", fieldType:FIELD_ITEM_TYPE.FIELD_ITEM_LEAF, parent:this.rootFieldItem.arrChildFieldItem[2],fieldValue:"cc2f", fieldSize:5})
]

Below is how I do the stringify and then parse it to get back the obj. And via this obj , i made the above observation.
I suspect there might need to configure some TD options for that to work. But I could not find any documentation for the options

 const strTemplate = TD.stringify(this.rootFieldItem)  
 const obj =TD.parse(strTemplate)
 

About the name of the npm library

Hi Jianwu,

I hope this message finds you well.

My name is Germán, and I am currently working on a new CRDT (Conflict-Free Replicated Data Type) library, similar to yjs. I'm interested in naming my project "treedoc" because it fits perfectly with its nature.

I noticed that you are the current owner of the "treedoc" package on npm. Since I only see your activity in this repository, I was wondering if you would be willing to free up the "treedoc" name on npm so that I could use it for my CRDT library. This would greatly help in aligning the project's identity with its intended purpose and making it easily discoverable for developers interested in CRDTs.

I would greatly appreciate your kindness and understanding in this matter. Thank you very much for considering my request. I look forward to your response.

Best,
Germán

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.