我正在尝试在Monotouch/Monodevelop上为IOS编写WCF服务.我使用[DataMember]/[DataContract]等标准属性作为我的可序列化对象,使用[ServiceContract]/[OperationContract]作为我的界面.一切正常,但是当我尝试在接口实现(服务器端)上实现返回IEnumerable的方法时,它没有用.
所以为了解决我的问题,我尝试使用最新版本的protobuf-net protobuf-net v2 beta r404.但我仍然从Protobuf-net收到序列化错误.请注意,"MyObject"中的IEnumerable序列化没有问题.
以下是我的代码现在的样子:
为MyObject:
[ProtoContract]
public class MyObject
{
public MyObject ()
{
}
[ProtoMember(1)]
public int Id {get;set;}
[ProtoMember(2)]
public IEnumerable<MyObject> myObjects {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
我的界面(Windows上的服务器端):
[ServiceContract]
public interface ITouchService
{
[OperationContract, ProtoBehavior]
MyObject Execute();
[OperationContract, ProtoBehavior]
IEnumerable<MyObject> ExecuteENUM ();
}
Run Code Online (Sandbox Code Playgroud)
我的界面(IOS上的客户端,我无法添加ProtoBehavior作为属性,因为它不在protobuf的ISO dll中):
[ServiceContract]
public interface ITouchService
{
[OperationContract]
MyObject Execute();
[OperationContract]
IEnumerable<MyObject> ExecuteENUM ();
}
Run Code Online (Sandbox Code Playgroud)
我的界面实现:
public class TouchService : ITouchService
{
public MyObject Execute()
{
var myObject = new MyObject() { Id = 9001 …Run Code Online (Sandbox Code Playgroud) 我是django的新手.任何人都可以帮助我...如何使用Rest Framework API上传文件?
我试过这个页面:
http://www.django-rest-framework.org/api-guide/parsers/#fileuploadparser
我正在尝试使用AM :: Serializer 构建JSON API样式API.我遇到了侧载问题.
我希望能够构建如下所示的JSON:
{
"primaries": [{
"id": 123,
"data": "Hello world.",
"links": {
"secondaries": [ 1, 2, 3 ]
}
}],
"linked" : {
"secondaries": [
{
"id": 1,
"data": "test1"
},
{
"id": 2,
"data": "test2"
},
{
"id": 3,
"data": "test3"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我能够提出的代码如下:
class PrimarySerializer < ActiveModel::Serializer
attributes :id, :data
has_many :secondaries, key: :secondaries, root: :secondaries
embed :ids, include: true
end
Run Code Online (Sandbox Code Playgroud)
这会生成如下所示的JSON:
{
"primaries": [{
"id": 123,
"data": "Hello world.", …Run Code Online (Sandbox Code Playgroud) 使用Python 2.7,
我使用从multiprocessing.managers派生的管理器跨进程传递许多大对象.BaseManager我想用cPickle串行器来节省时间; 如何才能做到这一点?我看到BaseManager初始化程序接受一个serializer参数,但唯一的选项似乎是pickle和xmlrpclib.
我是DJango的新手,我希望使用rest框架中的序列化器将数据添加到多对多字段中。
我的模特:
class IngFamily(models.Model):
name=models.CharField(max_length=30, unique=True, verbose_name='ingredient parent')
class UserProfile(models.Model):
user=models.ForeignKey(User)
allergies=models.ManyToManyField(IngFamily, verbose_name='allergies', null=True)
Run Code Online (Sandbox Code Playgroud)
现在,我想使用其余的api将数据添加到UserProfile模型。
我环顾四周,但找不到任何地方。
到目前为止,我已经实现了:
序列化器:
class IngFlySerializer(serializers.ModelSerializer):
class Meta:
model = IngFamily
fields= ('name',)
class UserProfileSerializer(serializers.ModelSerializer):
allergies=IngFlySerializer(many=True,)
class Meta:
model = UserProfile
fields=('allergies',)
def create(self, validated_data):
allergies_data =validated_data.pop('allergies', [])
#import pdb;pdb.set_trace()
user1=UserProfile.objects.create(**validated_data)
for allergy in allergies_data:
al=IngFamily.objects.filter(name=allergy.get('name'))
#al.name=allergy.get('name')
user1.allergies.add(al)
user1.save()
return user1
Run Code Online (Sandbox Code Playgroud)
当我尝试使用它时,“ al”为空。
视图:
class user_profile(generics.ListCreateAPIView):
serializer_class = UserProfileSerializer
permission_classes = (permissions.IsAuthenticated,)
def get_queryset(self):
user = self.request.user
return UserProfile.objects.filter(user=user)
def perform_create(self, serializer):
#import pdb; pdb.set_trace()
serializer.save(user=self.request.user) …Run Code Online (Sandbox Code Playgroud) python django serializer manytomanyfield django-rest-framework
我使用DataContractJsonSerializer将json字符串解析为对象hierarchie.json字符串如下所示:
{
"groups": [
{
"attributes": [
{
"sortOrder": "1",
"value": "A"
},
{
"sortOrder": "2",
"value": "B"
}
]
},
{
"attributes": {
"sortOrder": "1",
"value": "C"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
如您所见,"attributes"的子值可以是数组或单个项.我找到了问题发生的代码部分:
[DataContract]
public class ItemGroup
{
[DataMember(Name="attributes")]
public List<DetailItem> Items { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这适用于第一个,但在第二个上失败.
有没有人为此答案?
谢谢
我正在学习Android应用程序开发课程,并尝试创建并编写一个xml文件到android上的内部存储.就方法而言,我最初如何设置这个问题.我写了大部分但有错误,我无法弄清楚.也许是因为我一整天都在努力,我不知道.这是我这堂课的代码.我得到的错误是公共字符串宝藏和FileOutputStream上的非法修饰符.任何帮助,将不胜感激.
好的,我想出了使用try/catch所需的初始问题.能够运行,一切正常,直到我到达保存文件.现在出错:
SoundPool error loading/system./media./audio./ui/KeypressReturn.ogg.
AudioService Soundpool could not load file: /system/media/audio/ui/KeypressReturnj.ogg
Run Code Online (Sandbox Code Playgroud)
这是在"创建文件"写入日志之后.我猜它试图写错了文件?需要它写入/ data/data.我的应用中没有音频.我在下面添加了新代码:
旧代码:
public void onSaveTreasureClick(View v) throws FileNotFoundException{
Log.v("SaveTreasure","Button was clicked");
File f = new File(getFilesDir(),"treasure.xml");
FileOutputStream myFile=openFileOutput(f);
Log.v("WriteFile","file created");
private FileOutputStream openFileOutput(File f) {
// TODO Auto-generated method stub
return null;
}
public String treasures(Treasure treasure) throws Exception{
XmlSerializer xmlSerializer = Xml.newSerializer();
StringWriter write = new StringWriter();
final EditText tres=(EditText) findViewById(R.id.treasureNametxt);
String treasureName=tres.getText().toString();
final EditText c1=(EditText) findViewById(R.id.clue1Txt);
String clue1=c1.getText().toString();
final EditText c2=(EditText) findViewById(R.id.clue2Txt);
String clue2=c2.getText().toString();
final …Run Code Online (Sandbox Code Playgroud) https://github.com/dockyard/postgres_ext-serializers
这似乎很容易设置,但我似乎无法获得任何基本功能来将JSON从rails移动到postgres.我尝试在我的ams init,我的特定序列化程序和我的模型中包含以下内容,但它似乎永远不会激活.
我在Rails 4.2.3和Ruby 2.2上
这就是我尝试添加到多个文件:
需要'postgres_ext/serializers'
非常感谢你的帮助,我知道我必须遗漏一些明显的东西.
更新:为了给出更多的上下文,你读了这个gem的README.md指令,它只是说"
只需要'postgres_ext/serializers'并像往常一样使用ActiveModel :: Serializers!
所以我在我的application.rb中添加了require'postgres_ext/serializers',对序列化器进行了一个小编辑,看它是否有效:
class UserSerializer < ActiveModel::Serializer
cached false
attributes :id, :username, :location, :full_name
def full_name
"#{object.first_name} #{object.last_name}"
end
def full_name__sql
"first_name || ' ' || email"
end
end
Run Code Online (Sandbox Code Playgroud)
然后我将在我的Rails控制台中运行以下代码:
users = User.first(10)
ActiveModel::ArraySerializer.new(users, each_serializer: UserSerializer).to_json
Run Code Online (Sandbox Code Playgroud)
但__sql全名属性从未显示过,它似乎没有从postgres中提取数据与以前有任何不同.
这就是我的application.rb看起来像:
# require 'postgres_ext/serializers' ### Doesn't work here
require File.expand_path('../boot', __FILE__)
require 'rails/all'
require 'postgres_ext/serializers'
Bundler.require(*Rails.groups)
module Baller
class Application < Rails::Application
# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks …Run Code Online (Sandbox Code Playgroud) 我有一个带有Double []变量的简单用户类,用于指定用户的位置。
@Document
public class User {
private long id;
private Double[] location;
}
Run Code Online (Sandbox Code Playgroud)
这是我尝试序列化我的User对象的代码
new JSONSerializer()
.transform(new ArrayTransformer(), Double[].class)
.serialize(object));
Run Code Online (Sandbox Code Playgroud)
但是location字段不会被序列化,但是其他字段也会被序列化..有人可以帮忙吗?
谢谢!
有人可以用Symfony序列化器序列化来自多个类的数据时,如何解释我如何使用多个标准化器?
可以说我有以下课程:
class User
{
private $name;
private $books;
public function __construct()
{
$this->books = new ArrayCollection();
}
// getters and setters
}
class Book
{
private $title;
public function getTitle()
{
return $this->title;
}
public function setTitle($title)
{
$this->title = $title;
}
}
Run Code Online (Sandbox Code Playgroud)
我想序列化拥有多本书的用户。
$first = new Book();
$first->setTitle('First book');
$second = new Book();
$second->setTitle('Second book');
$user = new User();
$user->setName('Person name');
$user->addBook($first);
$user->addBook($second);
dump($this->get('serializer')->serialize($user, 'json'));
die();
Run Code Online (Sandbox Code Playgroud)
假设我还想在对书籍进行序列化时包含哈希,因此我有以下规范化器:
class BookNormalizer implements NormalizerInterface
{
public function normalize($object, $format = null, …Run Code Online (Sandbox Code Playgroud) serializer ×10
json ×3
python ×3
arrays ×2
c# ×2
django ×2
java ×2
activemodel ×1
android ×1
datacontract ×1
file ×1
flexjson ×1
ienumerable ×1
php ×1
pickle ×1
protobuf-net ×1
symfony ×1
wcf ×1
xml ×1