@@ -495,7 +495,14 @@ fn (mut g Gen) gen_sumtype_enc_dec(utyp ast.Type, sym ast.TypeSymbol, mut enc st
495495 dec.writeln ('\t\t\t if (strcmp("Time", ${type_var }) == 0) {' )
496496 gen_js_get (ret_styp, tmp, 'value' , mut dec, true )
497497 dec.writeln ('\t\t\t\t ${variant_typ } ${tmp } = time__unix(${js_dec_name ('i64 ')}(jsonroot_${tmp }));' )
498- dec.writeln ('\t\t\t\t ${prefix }res = ${variant_typ }_to_sumtype_${sym .cname }(&${tmp });' )
498+ if utyp.has_flag (.option) {
499+ dec.writeln ('\t\t\t\t ${prefix }res.state = 0;' )
500+ tmp_time_var := g.new_tmp_var ()
501+ dec.writeln ('\t\t\t\t ${g .base_type (utyp )} ${tmp_time_var } = ${variant_typ }_to_sumtype_${sym .cname }(&${tmp });' )
502+ dec.writeln ('\t\t\t\t vmemcpy(&${prefix }res.data, ${tmp_time_var }._time__Time, sizeof(${variant_typ }));' )
503+ } else {
504+ dec.writeln ('\t\t\t\t ${prefix }res = ${variant_typ }_to_sumtype_${sym .cname }(&${tmp });' )
505+ }
499506 dec.writeln ('\t\t\t }' )
500507 } else if ! is_js_prim (variant_typ) && variant_sym.kind != .enum {
501508 dec.writeln ('\t\t\t if (strcmp("${unmangled_variant_name }", ${type_var }) == 0 && ${variant_sym .kind == .array } == cJSON_IsArray(root)) {' )
@@ -771,11 +778,20 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
771778 tmp := g.new_tmp_var ()
772779 gen_js_get (styp, tmp, name, mut dec, is_required)
773780 dec.writeln ('\t if (jsonroot_${tmp }) {' )
774- dec.writeln ('\t\t ${prefix }${op }${c_name (field .name )} = time__unix(json__decode_u64(jsonroot_${tmp }));' )
775- if field.has_default_expr {
776- dec.writeln ('\t } else {' )
777- dec.writeln ('\t\t ${prefix }${op }${c_name (field .name )} = ${g .expr_string_opt (field .typ ,
778- field .default_expr )};' )
781+ if field.typ.has_flag (.option) {
782+ dec.writeln ('\t\t if (!(cJSON_IsNull(jsonroot_${tmp }))) {\n ' )
783+ dec.writeln ('\t\t\t ${prefix }${op }${c_name (field .name )}.state = 0;\n ' )
784+ tmp_time_var := g.new_tmp_var ()
785+ dec.writeln ('\t\t\t ${g .base_type (field .typ )} ${tmp_time_var } = time__unix(json__decode_u64(jsonroot_${tmp }));\n ' )
786+ dec.writeln ('\t\t\t vmemcpy(&${prefix }${op }${c_name (field .name )}.data, &${tmp_time_var }, sizeof(${g .base_type (field .typ )}));' )
787+ dec.writeln ('\t\t }\n ' )
788+ } else {
789+ dec.writeln ('\t\t ${prefix }${op }${c_name (field .name )} = time__unix(json__decode_u64(jsonroot_${tmp }));' )
790+ if field.has_default_expr {
791+ dec.writeln ('\t } else {' )
792+ dec.writeln ('\t\t ${prefix }${op }${c_name (field .name )} = ${g .expr_string_opt (field .typ ,
793+ field .default_expr )};' )
794+ }
779795 }
780796 dec.writeln ('\t }' )
781797 } else if field_sym.kind == .alias {
@@ -937,7 +953,11 @@ fn (mut g Gen) gen_struct_enc_dec(utyp ast.Type, type_info ast.TypeInfo, styp st
937953 if field_sym.name == 'time.Time' {
938954 // time struct requires special treatment
939955 // it has to be encoded as a unix timestamp number
940- enc.writeln ('${indent }cJSON_AddItemToObject(o, "${name }", json__encode_u64(${prefix_enc }${op }${c_name (field .name )}.__v_unix));' )
956+ if is_option {
957+ enc.writeln ('${indent }cJSON_AddItemToObject(o, "${name }", json__encode_u64((*(${g .base_type (field .typ )}*)(${prefix_enc }${op }${c_name (field .name )}.data)).__v_unix));' )
958+ } else {
959+ enc.writeln ('${indent }cJSON_AddItemToObject(o, "${name }", json__encode_u64(${prefix_enc }${op }${c_name (field .name )}.__v_unix));' )
960+ }
941961 } else {
942962 if ! field.typ.is_any_kind_of_pointer () {
943963 if field_sym.kind == .alias && field.typ.has_flag (.option) {
0 commit comments