[Falcor] Intro to JSON Graph

JSON is a very commonly used data interchange format. Unfortunately while most application domain models are graphs, JSON is designed to model hierarchical information. To get around this problem, Falcor introduces JSON Graph. JSON Graph introduces references to JSON, allowing you to ensure that no object appears more than once in your JSON.

 

Let say we have a json data to repersent a recently watched list and newly relesase list, in both lists, there is one item is the same:

复制代码
genreList: [
                    {
                        name: 'Recently Watched',
                        titles: [
                            {
                                id: 5221,
                                name: "House of Cards",
                                rating: 4.5
                            }
                        ]
                    },
                    {
                        name: "New Releases",
                        titles: [
                            {
                                id: 5221,
                                name: "House of Cards",
                                rating: 4.5
                            }
                        ]
                    }
                ]
复制代码

 

Then in recently watched list, I want to give "House of Cards" 5 rating:

        model.setValue('genreList[0].titles[0].rating', 5)
                .then(function (value) {
                    model.get('genreList[0..1].titles[0]["name","rating"]')
                            .then(function (json) {
                                console.log(JSON.stringify(json, null, 3));
                            })
                });

Here we set the rating as '5', then we print out the list again:

复制代码
{
   "json": {
      "genreList": {
         "0": {
            "titles": {
               "0": {
                  "name": "House of Cards",
                  "rating": 5
               }
            }
         },
         "1": {
            "titles": {
               "0": {
                  "name": "House of Cards",
                  "rating": 4.5
               }
            }
         }
      }
   }
}
复制代码

 

As we can see from the result, the first item is rated as 5, but the second item still renaming 4.5 . But they reperesnt the same item, what we want my rating can affect the item's rating all lists. 

 

But with json, it is hard to do that. That's why we need JSON Graph.

 

You can convert a json object into a jon graph in two step:

  1. Each json repersent item should have a unqie id, then you can use the unqie id as key to map object.
  2. Replace the original data with the json graph ref.
复制代码
cache: {
                titleById: {
                    5221: {
                       // id: 5221,
                        name: "House of Cards",
                        rating: 4.5
                    }
                },
                genreList: [
                    {
                        name: 'Recently Watched',
                        titles: [
                            {$type: 'ref', value: "titleById[5221]"}
                        ]
                    },
                    {
                        name: "New Releases",
                        titles: [
                            {$type: 'ref', value: "titleById[5221]"}
                        ]
                    }
                ]
            }
复制代码

5221 is the unqie id to repersent the json object. 

 

Actually there is a helper method can help to refactor the code:

复制代码
cache: {
                titleById: {
                    5221: {
                       // id: 5221,
                        name: "House of Cards",
                        rating: 4.5
                    }
                },
                genreList: [
                    {
                        name: 'Recently Watched',
                        titles: [
                            $ref("titleById[5221]")
                        ]
                    },
                    {
                        name: "New Releases",
                        titles: [
                            $ref("titleById[5221]")
                        ]
                    }
                ]
            }
复制代码

 

----------------------------------------

 

复制代码
<!-- index.html -->
<html>
<head>
    <!-- Do _not_  rely on this URL in production. Use only during development.  -->
    <script src="//netflix.github.io/falcor/build/falcor.browser.js"></script>
    <script>
        var $ref = falcor.Model.ref;
        var model = new falcor.Model({
            cache: {
                titleById: {
                    5221: {
                       // id: 5221,
                        name: "House of Cards",
                        rating: 4.5
                    }
                },
                genreList: [
                    {
                        name: 'Recently Watched',
                        titles: [
                            $ref("titleById[5221]")
                        ]
                    },
                    {
                        name: "New Releases",
                        titles: [
                            $ref("titleById[5221]")
                        ]
                    }
                ]
            }
        });

        model.setValue('genreList[0].titles[0].rating', 5)
                .then(function (value) {
                    model.get('genreList[0..1].titles[0]["name","rating"]')
                            .then(function (json) {
                                console.log(JSON.stringify(json, null, 3));
                            })
                });
    </script>
</head>
<body>
</body>
</html>
复制代码

 

posted @   Zhentiw  阅读(328)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2014-11-21 [ES6] 09. Destructuring Assignment -- 2
2014-11-21 [ES6] 08. Destructuring Assignment -- 1
2013-11-21 8. 利用反射机制, ListArray,intent来实现多Activity的切换
2013-11-21 7. Add song to Phone
2013-11-21 6. Activity life cycle
点击右上角即可分享
微信分享提示