1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.serialization.impl;
14 import java.io.DataInput;
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.List;
19 import org.simantics.databoard.Bindings;
20 import org.simantics.databoard.binding.Binding;
21 import org.simantics.databoard.binding.VariantBinding;
22 import org.simantics.databoard.binding.error.BindingException;
23 import org.simantics.databoard.serialization.Serializer;
24 import org.simantics.databoard.serialization.SerializerScheme;
25 import org.simantics.databoard.type.Datatype;
28 * A version of VariantSerializer that uses Bindings.getMutableBinding instead
29 * of Bindings.getBinding for deserialization.
31 * @author Tuukka Lehtonen
33 public class MutableVariantSerializer extends VariantSerializer {
35 public MutableVariantSerializer(VariantBinding binding, SerializerScheme scheme) {
36 super(binding, scheme);
40 public Object deserialize(DataInput in, List<Object> identities) throws IOException {
42 List<Object> typeIdentities = new ArrayList<Object>(1);
43 Datatype type = (Datatype) datatypeSerializer.deserialize(in, typeIdentities);
44 Binding valueBinding = Bindings.getMutableBinding(type);
45 Serializer valueSerializer = scheme.getSerializerUnchecked(valueBinding);
46 assertRemainingBytes(in, valueSerializer.getMinSize());
47 Object value = valueSerializer.deserialize(in, identities);
48 return binding.create(valueBinding, value);
49 } catch (BindingException e) {
50 throw new IOException( e );
55 public void deserializeTo(DataInput in, List<Object> identities, Object obj) throws IOException {
57 List<Object> typeIdentities = new ArrayList<Object>(1);
58 Datatype type = (Datatype) datatypeSerializer.deserialize(in, typeIdentities);
59 Datatype oldType = binding.getContentType(obj);
61 if (type.equals(oldType)) {
62 Binding valueBinding = binding.getContentBinding(obj);
63 Serializer valueSerializer = scheme.getSerializerUnchecked(valueBinding);
64 Object component = binding.getContent(obj);
65 // assertRemainingBytes(in, valueSerializer.getMinSize());
66 component = valueSerializer.deserializeToTry(in, identities, component);
67 binding.setContent(obj, valueBinding, component);
69 Binding valueBinding = Bindings.getMutableBinding(type);
70 Serializer valueSerializer = scheme.getSerializerUnchecked(valueBinding);
71 // assertRemainingBytes(in, valueSerializer.getMinSize());
72 Object component = valueSerializer.deserialize(in, identities);
73 binding.setContent(obj, valueBinding, component);
75 } catch (BindingException e) {
76 throw new IOException( e );
81 public void skip(DataInput in, List<Object> identities) throws IOException {
82 List<Object> typeIdentities = new ArrayList<Object>(1);
83 Datatype type = (Datatype) datatypeSerializer.deserialize(in, typeIdentities);
84 Binding valueBinding = Bindings.getMutableBinding(type);
85 Serializer valueSerializer = scheme.getSerializerUnchecked(valueBinding);
86 valueSerializer.skip(in, identities);