<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>jsonmodel &#8211; 编程技术记录</title>
	<atom:link href="https://blog.z6z8.cn/tag/jsonmodel/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.z6z8.cn</link>
	<description>世界你好!</description>
	<lastBuildDate>Mon, 28 Oct 2019 04:48:44 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
	<item>
		<title>Python JSONModel的实现思路</title>
		<link>https://blog.z6z8.cn/2019/10/28/python-jsonmodel%e7%9a%84%e5%ae%9e%e7%8e%b0%e6%80%9d%e8%b7%af/</link>
					<comments>https://blog.z6z8.cn/2019/10/28/python-jsonmodel%e7%9a%84%e5%ae%9e%e7%8e%b0%e6%80%9d%e8%b7%af/#respond</comments>
		
		<dc:creator><![CDATA[holdsky]]></dc:creator>
		<pubDate>Mon, 28 Oct 2019 04:48:44 +0000</pubDate>
				<category><![CDATA[python]]></category>
		<category><![CDATA[jsonmodel]]></category>
		<guid isPermaLink="false">http://blog.z6z8.cn/?p=527</guid>

					<description><![CDATA[JSONModel，模型和JSON数据之间相互转换，或者模型和JSON数据的序列化、反序列化。 不同语言实现细 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p>JSONModel，模型和JSON数据之间相互转换，或者模型和JSON数据的序列化、反序列化。</p>
<p>不同语言实现细节不一样，也并不是所有语言都可以实现。这里我参考Objective-C语言的JSONModel实现机制，设计了一种Python运行时JSON和Model的互转方案思路。</p>
<h1>object类</h1>
<p>用dir打印object</p>
<pre><code class="language-shell">&gt;&gt;&gt; dir(object)

['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']</code></pre>
<p>挨个找，看到两个方法<code>__setattr__</code>和<code>__getattribute__</code>，看名字也大概知道用途,基本意思就是可以通过<code>__setattr__</code>和<code>__getattribute__ </code> 访问object实例的属性值。</p>
<p>具体可参考https://www.cnblogs.com/elie/p/6685429.html</p>
<ul>
<li>
<p><code>__setattr__</code>   如果类自定义了<code>__setattr__</code>方法，当通过实例获取属性尝试赋值时，就会调用<code>__setattr__</code>。常规的对实例属性赋值，被赋值的属性和值会存入实例属性字典<code>__dict__</code>中；如下类自定义了<code>__setattr__</code>,对实例属性的赋值就会调用它。类定义中的self.attr也同样，所以在<code>__setattr__</code>下还有self.attr的赋值操作就会出现无线递归的调用<code>__setattr__</code>的情况。自己实现<code>__setattr__</code>有很大风险，一般情况都还是继承object类的<code>__setattr__</code>方法。</p>
</li>
<li>
<p><code>__getattribute__ </code>         实例instance通过instance.name访问属性name，<code>__getattribute__</code>方法一直会被调用，无论属性name是否追溯到。如果类还定义了<code>__getattr__</code>方法，除非通过<code>__getattribute__</code>显式的调用它，或者<code>__getattribute__</code>方法出现AttributeError错误，否则<code>__getattr__</code>方法不会被调用了。如果在<code>__getattribute__(self, attr)</code>方法下存在通过self.attr访问属性，会出现无限递归错误。</p>
</li>
</ul>
<h1>步骤</h1>
<p>只讨论最小场景（当然可以自行扩展）</p>
<h2>json -&gt; model</h2>
<p>这里假设json是一个字典，且没有嵌套情况。<br />
并且假设应知道model的类名，一个字典对应一个model。</p>
<p>遍历json所有key ,然后按照key名称给model的属性赋值。</p>
<p>示意代码</p>
<pre><code class="language-python">dict = json数据
model=类的实例
for (key,value) in dict.items():
            model.__setattr__(key,value)</code></pre>
<h2>model -&gt; json</h2>
<p>这里假设model的所有属性都能转化为对应的json数据，且没有嵌套。<br />
这里，还需要借助<code>__dict__</code>函数</p>
<p>示意代码</p>
<pre><code class="language-python">dict = {}
for (name,_) in model.__dict__.items():
    value = model.__getattribute__(name)
    if value != None:
        dict[name] = value</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.z6z8.cn/2019/10/28/python-jsonmodel%e7%9a%84%e5%ae%9e%e7%8e%b0%e6%80%9d%e8%b7%af/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
