Class: RGen::Serializer::JsonSerializer
- Inherits:
-
Object
- Object
- RGen::Serializer::JsonSerializer
- Defined in:
- lib/rgen/serializer/json_serializer.rb
Instance Method Summary collapse
- #attributeValue(value, a) ⇒ Object
- #elementIdentifier(element) ⇒ Object
-
#initialize(writer, opts = {}) ⇒ JsonSerializer
constructor
A new instance of JsonSerializer.
-
#qualifiedElementName(element) ⇒ Object
simple identifier calculation based on qualified names prerequisits: * containment relations must be bidirectionsl * local name stored in single attribute @identAttrName for all classes.
- #serialize(elements) ⇒ Object
- #serializeElement(element, indent = "") ⇒ Object
- #writeFeature(feat, value, indent) ⇒ Object
Constructor Details
#initialize(writer, opts = {}) ⇒ JsonSerializer
Returns a new instance of JsonSerializer.
7 8 9 10 11 12 13 14 15 |
# File 'lib/rgen/serializer/json_serializer.rb', line 7 def initialize(writer, opts={}) @writer = writer @elementIdentifiers = {} @identAttrName = opts[:identAttrName] || "name" @separator = opts[:separator] || "/" @leadingSeparator = opts.has_key?(:leadingSeparator) ? opts[:leadingSeparator] : true @featureFilter = opts[:featureFilter] @identifierProvider = opts[:identifierProvider] end |
Instance Method Details
#attributeValue(value, a) ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/rgen/serializer/json_serializer.rb', line 102 def attributeValue(value, a) if a.eType == RGen::ECore::EString || a.eType.is_a?(RGen::ECore::EEnum) "\""+value.to_s.gsub('\\','\\\\\\\\').gsub('"','\\"').gsub("\n","\\n").gsub("\r","\\r"). gsub("\t","\\t").gsub("\f","\\f").gsub("\b","\\b")+"\"" else value.to_s end end |
#elementIdentifier(element) ⇒ Object
17 18 19 20 |
# File 'lib/rgen/serializer/json_serializer.rb', line 17 def elementIdentifier(element) ident = @identifierProvider && @identifierProvider.call(element) ident || (element.is_a?(RGen::MetamodelBuilder::MMProxy) && element.targetIdentifier) || qualifiedElementName(element) end |
#qualifiedElementName(element) ⇒ Object
simple identifier calculation based on qualified names prerequisits:
-
containment relations must be bidirectionsl
-
local name stored in single attribute @identAttrName for all classes
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/rgen/serializer/json_serializer.rb', line 27 def qualifiedElementName(element) return @elementIdentifiers[element] if @elementIdentifiers[element] localIdent = ((element.respond_to?(@identAttrName) && element.getGeneric(@identAttrName)) || "").strip parentRef = element.class.ecore.eAllReferences.select{|r| r.eOpposite && r.eOpposite.containment}.first parent = parentRef && element.getGeneric(parentRef.name) if parent if localIdent.size > 0 parentIdent = qualifiedElementName(parent) result = parentIdent + @separator + localIdent else result = qualifiedElementName(parent) end else result = (@leadingSeparator ? @separator : "") + localIdent end @elementIdentifiers[element] = result end |
#serialize(elements) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rgen/serializer/json_serializer.rb', line 45 def serialize(elements) if elements.is_a?(Array) write("[ ") elements.each_with_index do |e, i| serializeElement(e) write(",\n") unless i == elements.size-1 end write("]") else serializeElement(elements) end end |
#serializeElement(element, indent = "") ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/rgen/serializer/json_serializer.rb', line 58 def serializeElement(element, indent="") write(indent + "{ \"_class\": \""+element.class.ecore.name+"\"") element.class.ecore.eAllStructuralFeatures.each do |f| next if f.derived value = element.getGeneric(f.name) unless value == [] || value.nil? || (f.is_a?(RGen::ECore::EReference) && f.eOpposite && f.eOpposite.containment) || (@featureFilter && !@featureFilter.call(f)) write(", ") writeFeature(f, value, indent) end end write(" }") end |
#writeFeature(feat, value, indent) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/rgen/serializer/json_serializer.rb', line 73 def writeFeature(feat, value, indent) write("\""+feat.name+"\": ") if feat.is_a?(RGen::ECore::EAttribute) if value.is_a?(Array) write("[ "+value.collect{|v| attributeValue(v, feat)}.join(", ")+" ]") else write(attributeValue(value, feat)) end elsif !feat.containment if value.is_a?(Array) write("[ "+value.collect{|v| "\""+elementIdentifier(v)+"\""}.join(", ")+" ]") else write("\""+elementIdentifier(value)+"\"") end else if value.is_a?(Array) write("[ \n") value.each_with_index do |v, i| serializeElement(v, indent+" ") write(",\n") unless i == value.size-1 end write("]") else write("\n") serializeElement(value, indent+" ") end end end |