Don't create attributes if the column i...

2011-07-13
2013-05-28
  • Marcelo F. Ochoa

    Hi:
       If the column value is null this patch for json_dyn.sql allows complete ignore them, this is useful with the Solr JSon handler which not allows attributes with null values.

    Index: json_dyn.sql

    -- json_dyn.sql        (revision 37)
    +++ json_dyn.sql        (working copy)
    @@ -21,6 +21,7 @@
       THE SOFTWARE.
       */

    +  ignore_null            boolean not null := true;  - do not add attributes with null
       null_as_empty_string   boolean not null := true;  -varchar2
       include_dates          boolean not null := true;  -date
       include_clobs          boolean not null := true;
    @@ -117,32 +118,31 @@
             when l_dtbl(i).col_type in (1,96) then - varchar2
               dbms_sql.column_value(l_cur,i,l_val);
               if(l_val is null) then
    -            if(null_as_empty_string) then
    -              inner_obj.put(l_dtbl(i).col_name, ''); -treatet as emptystring?
    -            else
    -              inner_obj.put(l_dtbl(i).col_name, json_value.makenull); -null
    +            if(not ignore_null) then
    +              if(null_as_empty_string) then
    +                inner_obj.put(l_dtbl(i).col_name, ''); -treatet as emptystring?
    +              else
    +                inner_obj.put(l_dtbl(i).col_name, json_value.makenull); -null
    +              end if;
                 end if;
               else
    -            declare
    -              v json_value;
    -            begin
    -              v := json_parser.parse_any('"'||l_val||'"');
    -              inner_obj.put(l_dtbl(i).col_name, v); -null
    -            exception when others then
    -              inner_obj.put(l_dtbl(i).col_name, json_value.makenull); -null
    -            end;
    +            inner_obj.put(l_dtbl(i).col_name, json_value(l_val)); - not null
               end if;
               -dbms_output.put_line(l_dtbl(i).col_name||' -> '||l_val||'varchar2' ||l_dtbl(i).col_type);
             -handling number types
             when l_dtbl(i).col_type = 2 then - number
               dbms_sql.column_value(l_cur,i,l_val);
    -          conv := l_val;
    -          inner_obj.put(l_dtbl(i).col_name, conv);
    +          if(l_val is not null or not ignore_null) then
    +            conv := l_val;
    +            inner_obj.put(l_dtbl(i).col_name, conv);
    +          end if;
               - dbms_output.put_line(l_dtbl(i).col_name||' -> '||l_val||'number ' ||l_dtbl(i).col_type);
             when l_dtbl(i).col_type = 12 then - date
               if(include_dates) then
                 dbms_sql.column_value(l_cur,i,read_date);
    -            inner_obj.put(l_dtbl(i).col_name, json_ext.to_json_value(read_date));
    +            if(read_date is not null or not ignore_null) then
    +              inner_obj.put(l_dtbl(i).col_name, json_ext.to_json_value(read_date));
    +            end if;
               end if;
               -dbms_output.put_line(l_dtbl(i).col_name||' -> '||l_val||'date ' ||l_dtbl(i).col_type);
             when l_dtbl(i).col_type = 112 then -clob

       Best regards, Marcelo.

     
  • James Sumners

    James Sumners - 2011-07-13

    Then their JSON parser is broken and you should file a bug report. The RFC clearly states in section 2.1 that 'null' is an appropriate value.

     
  • Marcelo F. Ochoa

    Hi:
       Sure the JSON specification allows null values, but here an example of JSON post valid for Solr:
    {
        "add" : {
              "doc" : {
                  "B" : 1,
                  "A" : "aa"
              }
       },
        "add" : {
             "doc" : {
                 "B" : 2,
                 "A" : "ab"
            }
       },
       "add" : {
            "doc" : {
                "A" : "ac"
            }
        }
    }

       Note that last value was generated for row inserted as:
          insert into bb values ('ac',null);
       Best regards and thanks a lot for your quick reply. Marcelo.

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks