谷歌应用程序引擎发送带有JSON数据太多 [英] Google App Engine sends too much data with JSON

查看:197
本文介绍了谷歌应用程序引擎发送带有JSON数据太多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了GAE和云端点一个非常简单的API,使用的数据存储。我已经做了一些属性的实体命名的书(其中大部分是字符串),并部署了应用程序。

I created a very simple API with GAE and Cloud Endpoints, using the Datastore. I have made an Entity named Book with some attributes (most of them are Strings) and deployed the application.

我的GET和POST请求正常工作,但是我不明白为什么每次我做一个GET请求的时间来找回我的数据存储区实体存在的JSON一个额外的行,例如:

My GET and POST requests work correctly, however I don't understand why each time I do a GET request to retrieve my entities in the datastore there is an extra line in the JSON, for instance:

{
        "id": "5634482569460976",
        "date": "20141125",
        "author": "Charly",
        "kind": "bookendpoint#resourcesItem"
    }

我从未在我的实体的厚道的属性,哪里是从何而来?
除了在每个JSON响应结束时,我能找到这些两行:

I never set a "kind" attribute in my Entity, where does that come from? Besides at the end of each json response I can find these two lines:

"kind": "bookendpoint#resources",
"etag": "\"dIDB-NLukmBT86-tBYjgZpbt2_Y/FcVIa289PJU7Cjr-bG8b0oxmfrKQ\""

我不知道那些种和ETAG会来自

I don't know where those "kind" and "etag" come from either.

你能告诉我这些是什么,以及如何从出现在JSON响应prevent他们?

Can you tell me what are these and how to prevent them from appearing in the JSON responses?

推荐答案

这两个属性是由云端点自动添加。我不知道如何删除它和谷歌似乎并没有说明为什么它们被添加的,但它的一些很容易猜到。

Both attributes are added automatically by Cloud Endpoints. I do not know how to remove it and Google does not seem to document why they are added, but some of it is easy to guess.

字段

The kind field

表示到Android客户端如何序列化(转换从JSON文本到Java对象)中的数据。与多态性发挥作用,这非常有用。例如想象一个你会从端点返回这个类:

kind indicates to the Android client how to serialize (transform from JSON text to Java object) the data. This is useful with polymorphism comes into play. For example imagine a that you will return this class from the endpoint :

public class Library {
  public List<Item> items;
}

public interface Item{
  private String title;
}

public class Book extends Item {
}

public class Magazine extends Item {
}

然后假设你的Andr​​oid客户端收到此JSON数据:

Then assume that your Android client receives this JSON data :

{
  "items": [
           {"title":"My book"},
           {"title":"My magazine"},
      ]
}

那么客户机如何知道哪些项目是图书键,这是杂志?这些类可以有在客户端完全不同的行为,因此重要的是要知道的。

Then how does the client know which item is a Book and which is " Magazine ? Those classes could have completely different behavior on the client side so it's important to know.

但是,如果您添加字段,序列化就能拿起右项目实施:

But if you add the kind field, the serializer will be able to pick up the right Item implementation :

{
  "kind": "bookendpoint#Library"
  "items": [
           {"title":"My book","kind": "bookendpoint#Book"},
           {"title":"My magazine","kind": "bookendpoint#Magazine"},
      ]
}

ETAG 字段

The etag field

这一个是最神秘的对我来说一点。在HTTP协议中,ETag是某种哈希的资源,是用来缓存客户端上的资源,并避免重新下载它,如果它没有改变的。

This one is a bit more mysterious to me. In the HTTP protocol, ETags are some kind of hashes for a resource, that are used to cache the resource on client side and avoid downloading it again if it has not changed.

通常,当它服务的资源,然后客户会问:给我的资源,除非ETag的XXX仍然有效的服务器将提供一个ETag。还是有参与的HTTP请求,但无需下载,除非资源已在服务器端更改。

Typically the server will provide an ETag when it serves the resource, and then the client will ask "Give me that resource, unless the ETag XXX is still valid". There's still an HTTP request involved but no download is required unless the resource has changed on the server side.

您可以了解更多关于这个维基百科页面

的ETag将与云端点伟大的使用。我猜测,云计算的端点它要返回响应的一些散。如果这个响应 ETAG匹配由云端点客户端提供的字段,没有数据返回,而不是 HTTP 304未修改发送响应。

ETags would be of great use with Cloud Endpoints. I am guessing that Cloud Endpoints computes some hash of the response it wants to return. If this response matches the etag field provided by the Cloud Endpoints client, no data is returned and instead an HTTP 304 Not Modified response is sent.

这篇关于谷歌应用程序引擎发送带有JSON数据太多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆