V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
sutar
V2EX  ›  iDev

JSON2Mantle:用 JSON 文件生成 Mantle model 的小工具

  •  
  •   sutar ·
    sutar · 2015-03-11 07:59:47 +08:00 · 5472 次点击
    这是一个创建于 3305 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Mantle 是一个构建 model 层的库。然而通过 Mantle 偷懒的同时,还是得自己把 JSON 里的字段写成 Objective-C 文件。为了进一步偷懒,我写了一个 Python 小工具:输入 xxx.json 自动生成相应的 Mantle model 文件。

    先放地址:https://github.com/sutar/JSON2Mantle

    安装:

    $ sudo pip install JSON2Mantle
    

    Features

    自动转换 property 名称

    • var_name 自动转换成 varName
    • Objective-C 中的保留字进行转换。比如 id 会转换成 modelId

    (半)自动类型映射

    目前能处理 NSInteger, NSString, BOOL 等基本类型。下一步可能会对 NSURL 等类型进行检测。

    支持多级 JSON 字典 (nested)

    例如如下的user数据:

    {
      "id": 10,
      "name": "Clementina DuBuque",
      "username": "Moriah.Stanton",
      "email": "[email protected]",
      "address": {
        "street": "Kattie Turnpike",
        "suite_number": "Suite 198",
        "city": "Lebsackbury",
        "zipcode": "31428-2261",
        "geo": {
          "lat": "-38.2386",
          "lng": "57.2232"
        }
      },
      "cell_phone": "024-648-3804",
      "website": "ambrose.net",
      "company": {
        "name": "Hoeger LLC",
        "catch_phrase": "Centralized empowering task-force",
        "bs": "target end-to-end models"
      }
    }
    

    只要一次执行,就能生成UserModel AddressModel GeoModelCompanyModel 四个 models。

    产生的UserModel.h文件如下:

    #import <Mantle.h>
    #import "AddressModel.h"
    #import "CompanyModel.h"
    
    @interface UserModel : MTLModel <MTLJSONSerializing>
    
    @property (nonatomic, copy) NSString *username;
    @property (nonatomic, copy) NSString *website;
    @property (nonatomic, copy) NSString *email;
    @property (nonatomic, copy) NSString *name;
    @property (nonatomic, assign) NSInteger modelId;
    @property (nonatomic, strong) AddressModel *address;
    @property (nonatomic, strong) CompanyModel *company;
    @property (nonatomic, copy) NSString *cellPhone;
    
    @end
    

    只要执行一句:

    $ json2mantle user.json output
    

    OK。介绍完毕,再放下地址: https://github.com/sutar/JSON2Mantle

    欢迎 star。

    17 条回复    2015-05-11 09:58:43 +08:00
    chon
        1
    chon  
       2015-03-11 10:21:35 +08:00
    好顶赞
    newtonisaac
        2
    newtonisaac  
       2015-03-11 11:28:56 +08:00
    加星
    zenghaojim33
        3
    zenghaojim33  
       2015-03-11 12:08:12 +08:00
    先顶再看
    Hysteria
        4
    Hysteria  
       2015-03-11 13:11:44 +08:00
    这种工具如果没问题确实有很大的帮助,赞楼主,先收藏了。
    minglei
        5
    minglei  
       2015-03-11 15:57:12 +08:00
    Nice
    sutar
        6
    sutar  
    OP
       2015-03-11 16:24:09 +08:00
    @Hysteria 所以得需要大家多多测试。。(保留字那里还没有完成。。
    Hysteria
        7
    Hysteria  
       2015-03-11 18:34:10 +08:00
    @sutar 最近用不着= - =,用到问题果断告诉你。
    Elethom
        8
    Elethom  
       2015-03-11 19:22:45 +08:00 via iPhone
    Name 和 organization 可以通過標準 API 從系統獲取。
    sutar
        9
    sutar  
    OP
       2015-03-11 19:38:43 +08:00
    @Elethom 了解了,thanks。
    em91
        10
    em91  
       2015-03-11 20:42:46 +08:00
    好顶赞
    sutar
        11
    sutar  
    OP
       2015-03-12 06:20:53 +08:00
    @Elethom 我发现用 Python 的话只能得到 current uid。然后用 defaults read 没找到相应的信息。。而且 organization 其实取决于项目吧?望赐教。。
    Elethom
        12
    Elethom  
       2015-03-12 06:40:29 +08:00 via iPhone
    @sutar
    import AddressBook
    zenghaojim33
        13
    zenghaojim33  
       2015-03-12 12:03:16 +08:00
    你好,我用你tests目录下的top10.json做测试报错了,请问正确的使用姿势是什么

    haodeiMac:tests pro$ json2mantle json2mantle top10.json class --prefix XYZ --author "John Smith"
    Traceback (most recent call last):
    File "/usr/local/bin/json2mantle", line 9, in <module>
    load_entry_point('JSON2Mantle==0.0.6', 'console_scripts', 'json2mantle')()
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 357, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 2394, in load_entry_point
    return ep.load()
    File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources.py", line 2108, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
    ImportError: No module named json2mantle.cli
    sutar
        14
    sutar  
    OP
       2015-03-12 16:29:29 +08:00
    @Elethom 原来如此,学习了。。 _(-ω-J∠)_


    @zenghaojim33 看起来像是没装好的样子,请检查:

    1)在 Python 中输入 import site; site.getsitepackages() 查看 site-packages 路径;
    2)并检查几个路径下是否有 json2mantle 文件夹。

    我在的系统里(OS X 10.10.2),位于 /Library/Python/2.7/site-packages/json2Mantle 。
    richardhc
        15
    richardhc  
       2015-03-13 08:35:29 +08:00
    好东西, 这些写json串比较方便的转为objc对象了...
    pheyer
        16
    pheyer  
       2015-04-28 19:37:32 +08:00
    有用,点个赞,如果能有gui更好了
    pheyer
        17
    pheyer  
       2015-05-11 09:58:43 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2724 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 15:39 · PVG 23:39 · LAX 08:39 · JFK 11:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.