Class: Hprose::Writer

Inherits:
Object
  • Object
show all
Includes:
Stream, Tags
Defined in:
lib/hprose/io.rb

Overview

class Reader

Defined Under Namespace

Classes: FakeWriterRefer, RealWriterRefer

Constant Summary

Constants included from Tags

Tags::TagArgument, Tags::TagBytes, Tags::TagCall, Tags::TagClass, Tags::TagClosebrace, Tags::TagDate, Tags::TagDouble, Tags::TagEmpty, Tags::TagEnd, Tags::TagError, Tags::TagFalse, Tags::TagFunctions, Tags::TagGuid, Tags::TagInfinity, Tags::TagInteger, Tags::TagList, Tags::TagLong, Tags::TagMap, Tags::TagNaN, Tags::TagNeg, Tags::TagNine, Tags::TagNull, Tags::TagObject, Tags::TagOpenbrace, Tags::TagPoint, Tags::TagPos, Tags::TagQuote, Tags::TagRef, Tags::TagResult, Tags::TagSemicolon, Tags::TagString, Tags::TagTime, Tags::TagTrue, Tags::TagUTC, Tags::TagUTF8Char, Tags::TagZero

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Stream

#readint, #readuntil

Constructor Details

#initialize(stream, simple = false) ⇒ Writer

Returns a new instance of Writer.



697
698
699
700
701
702
# File 'lib/hprose/io.rb', line 697

def initialize(stream, simple = false)
  @stream = stream
  @classref = {}
  @fieldsref = []
  @refer = (simple ? FakeWriterRefer.new : RealWriterRefer.new)
end

Instance Attribute Details

#streamObject

Returns the value of attribute stream.



703
704
705
# File 'lib/hprose/io.rb', line 703

def stream
  @stream
end

Instance Method Details

#resetObject



902
903
904
905
906
# File 'lib/hprose/io.rb', line 902

def reset
  @classref.clear
  @fieldsref.clear
  @refer.reset
end

#serialize(obj) ⇒ Object



704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
# File 'lib/hprose/io.rb', line 704

def serialize(obj)
  case obj
  when NilClass then @stream.putc(TagNull)
  when FalseClass then @stream.putc(TagFalse)
  when TrueClass then @stream.putc(TagTrue)
  when Fixnum then write_integer(obj)
  when Bignum then write_long(obj)
  when Float then write_double(obj)
  when String then
    len = obj.length
    if len == 0 then
      @stream.putc(TagEmpty)
    elsif (len < 4) and (obj.ulength == 1) then
      write_utf8char(obj)
    elsif not write_ref(obj) then
      if obj.utf8? then
        write_string(obj)
      else
        write_bytes(obj)
      end
    end
  when Symbol then write_string_with_ref(obj)
  when UUID then write_guid_with_ref(obj)
  when Time then write_date_with_ref(obj)
  when Array, Range, MatchData then write_list_with_ref(obj)
  when Hash then write_map_with_ref(obj)
  when Binding, Class, Dir, Exception, IO, Numeric,
      Method, Module, Proc, Regexp, Thread, ThreadGroup then
    raise Exception.exception('This type is not supported to serialize')
  else write_object_with_ref(obj)
  end
end

#write_boolean(bool) ⇒ Object



778
779
780
# File 'lib/hprose/io.rb', line 778

def write_boolean(bool)
  @stream.putc(bool ? TagTrue : TagFalse)
end

#write_bytes(bytes) ⇒ Object



804
805
806
807
808
809
810
811
812
# File 'lib/hprose/io.rb', line 804

def write_bytes(bytes)
  @refer.set(bytes)
  length = bytes.length
  @stream.putc(TagBytes)
  @stream.write(length.to_s) if length > 0
  @stream.putc(TagQuote)
  @stream.write(bytes)
  @stream.putc(TagQuote)
end

#write_bytes_with_ref(bytes) ⇒ Object



813
814
815
# File 'lib/hprose/io.rb', line 813

def write_bytes_with_ref(bytes)
  write_bytes(bytes) unless write_ref(bytes)
end

#write_date(time) ⇒ Object Also known as: write_time



781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
# File 'lib/hprose/io.rb', line 781

def write_date(time)
  @refer.set(time)
  if time.hour == 0 and time.min == 0 and time.sec == 0 and time.usec == 0 then
    @stream.putc(TagDate)
    @stream.write(time.strftime('%Y%m%d'))
    @stream.putc(time.utc? ? TagUTC : TagSemicolon)
  elsif time.year == 1970 and time.mon == 1 and time.day == 1 then
    @stream.putc(TagTime)
    @stream.write(time.strftime('%H%M%S'))
    write_usec(time.usec)
    @stream.putc(time.utc? ? TagUTC : TagSemicolon)
  else
    @stream.putc(TagDate)
    @stream.write(time.strftime('%Y%m%d' << TagTime << '%H%M%S'))
    write_usec(time.usec)
    @stream.putc(time.utc? ? TagUTC : TagSemicolon)
  end
end

#write_date_with_ref(time) ⇒ Object Also known as: write_time_with_ref



799
800
801
# File 'lib/hprose/io.rb', line 799

def write_date_with_ref(time)
  write_date(time) unless write_ref(time)
end

#write_double(double) ⇒ Object



754
755
756
757
758
759
760
761
762
763
764
# File 'lib/hprose/io.rb', line 754

def write_double(double)
  if double.nan? then
    write_nan
  elsif double.finite? then
    @stream.putc(TagDouble)
    @stream.write(double.to_s)
    @stream.putc(TagSemicolon)
  else
    write_infinity(double > 0)
  end
end

#write_emptyObject



775
776
777
# File 'lib/hprose/io.rb', line 775

def write_empty
  @stream.putc(TagEmpty)
end

#write_guid(guid) ⇒ Object



833
834
835
836
837
838
839
# File 'lib/hprose/io.rb', line 833

def write_guid(guid)
  @refer.set(guid)
  @stream.putc(TagGuid)
  @stream.putc(TagOpenbrace)
  @stream.write(guid.to_s)
  @stream.putc(TagClosebrace)
end

#write_guid_with_ref(guid) ⇒ Object



840
841
842
# File 'lib/hprose/io.rb', line 840

def write_guid_with_ref(guid)
  write_guid(guid) unless write_ref(guid)
end

#write_infinity(positive = true) ⇒ Object



768
769
770
771
# File 'lib/hprose/io.rb', line 768

def write_infinity(positive = true)
  @stream.putc(TagInfinity)
  @stream.putc(positive ? TagPos : TagNeg)
end

#write_integer(integer) ⇒ Object



736
737
738
739
740
741
742
743
744
# File 'lib/hprose/io.rb', line 736

def write_integer(integer)
  if (0..9) === integer then
    @stream.putc(integer.to_s)
  else
    @stream.putc((-2147483648..2147483647) === integer ? TagInteger : TagLong)
    @stream.write(integer.to_s)
    @stream.putc(TagSemicolon)
  end
end

#write_list(list) ⇒ Object



843
844
845
846
847
848
849
850
851
852
853
854
# File 'lib/hprose/io.rb', line 843

def write_list(list)
  @refer.set(list)
  list = list.to_a
  count = list.size
  @stream.putc(TagList)
  @stream.write(count.to_s) if count > 0
  @stream.putc(TagOpenbrace)
  count.times do |i|
    serialize(list[i])
  end
  @stream.putc(TagClosebrace)
end

#write_list_with_ref(list) ⇒ Object



855
856
857
# File 'lib/hprose/io.rb', line 855

def write_list_with_ref(list)
  write_list(list) unless write_ref(list)
end

#write_long(long) ⇒ Object



745
746
747
748
749
750
751
752
753
# File 'lib/hprose/io.rb', line 745

def write_long(long)
  if (0..9) === long then
    @stream.putc(long.to_s)
  else
    @stream.putc(TagLong)
    @stream.write(long.to_s)
    @stream.putc(TagSemicolon)
  end
end

#write_map(map) ⇒ Object



858
859
860
861
862
863
864
865
866
867
868
869
# File 'lib/hprose/io.rb', line 858

def write_map(map)
  @refer.set(map)
  size = map.size
  @stream.putc(TagMap)
  @stream.write(size.to_s) if size > 0
  @stream.putc(TagOpenbrace)
  map.each do |key, value|
    serialize(key)
    serialize(value)
  end
  @stream.putc(TagClosebrace)
end

#write_map_with_ref(map) ⇒ Object



870
871
872
# File 'lib/hprose/io.rb', line 870

def write_map_with_ref(map)
  write_map(map) unless write_ref(map)
end

#write_nanObject



765
766
767
# File 'lib/hprose/io.rb', line 765

def write_nan
  @stream.putc(TagNaN)
end

#write_nullObject



772
773
774
# File 'lib/hprose/io.rb', line 772

def write_null
  @stream.putc(TagNull)
end

#write_object(object) ⇒ Object



873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
# File 'lib/hprose/io.rb', line 873

def write_object(object)
  classname = ClassManager.getClassAlias(object.class)
  if @classref.key?(classname) then
    index = @classref[classname]
    fields, vars = @fieldsref[index]
  else
    if object.is_a?(Struct) then
      vars = nil
      fields = object.members
    else
      vars = object.instance_variables
      fields = vars.map { |var| var.to_s.delete('@') }
    end
    index = write_class(classname, fields, vars)
  end
  @stream.putc(TagObject)
  @stream.write(index.to_s)
  @stream.putc(TagOpenbrace)
  @refer.set(object)
  if vars.nil? then
    fields.each { |field| serialize(object[field]) }
  else
    vars.each { |var| serialize(object.instance_variable_get(var)) }
  end
  @stream.putc(TagClosebrace)
end

#write_object_with_ref(object) ⇒ Object



899
900
901
# File 'lib/hprose/io.rb', line 899

def write_object_with_ref(object)
  write_object(object) unless write_ref(object)
end

#write_string(string) ⇒ Object



820
821
822
823
824
825
826
827
828
829
# File 'lib/hprose/io.rb', line 820

def write_string(string)
  @refer.set(string)
  string = string.to_s
  length = string.ulength
  @stream.putc(TagString)
  @stream.write(length.to_s) if length > 0
  @stream.putc(TagQuote)
  @stream.write(string)
  @stream.putc(TagQuote)
end

#write_string_with_ref(string) ⇒ Object



830
831
832
# File 'lib/hprose/io.rb', line 830

def write_string_with_ref(string)
  write_string(string) unless write_ref(string)
end

#write_utf8char(utf8char) ⇒ Object



816
817
818
819
# File 'lib/hprose/io.rb', line 816

def write_utf8char(utf8char)
  @stream.putc(TagUTF8Char)
  @stream.write(utf8char)
end