<commit_before>/*
 * Copyright 2006-2008 The FLWOR Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "util/converters/json_converter.h"

#include "json/parser.h"
#include "json/value.h"

#include "system/globalenv.h"
#include "context/namespace_context.h"

namespace zorba {
  json::value* getValue(const char* aJSON, const unsigned int aLen);

  bool create_Node_Helper(store::Item_t parent,
                          xqpStringStore_t baseUri,
                          xqpStringStore_t name,
                          store::Item_t* result = NULL);

  bool create_Attribute_Helper( store::Item_t parent,
                                xqpStringStore_t name,
                                xqpStringStore_t value,
                                store::Item_t* result = NULL);

  bool create_Pair_Helper(store::Item_t parent,
                          xqpStringStore_t baseUri,
                          store::Item_t* result,
                          xqpStringStore_t name,
                          xqpStringStore_t type,
                          xqpStringStore_t value = NULL);

  void parse_Json_value(json::value** value,
                        store::Item_t parent,
                        xqpStringStore_t baseUri,
                        store::Item_t* result);

  void parse_Json_ML_value( json::value** value,
                            store::Item_t parent,
                            xqpStringStore_t baseUri,
                            store::Item_t* result);

  bool parse_element( const store::Item* element,
                      xqpStringStore_t& json_string,
                      xqpStringStore_t& error_log);

  bool parse_child( const store::Item* element,
                    xqpStringStore_t& json_string,
                    xqpStringStore_t& error_log);

  bool get_value( const store::Item* element,
                  xqpStringStore_t& value);

  bool JSON_parse(const char* aJSON_string, const unsigned int aLength,
                  store::Item_t& element, xqpStringStore_t baseUri,
                  xqp_string& error_log)
  {
    std::auto_ptr<json::value> lValue(getValue(aJSON_string, aLength));

    json::vector_list_t::iterator vectIter;
    json::vector_list_t *vect = lValue->getchildrenlist();
    if(vect != 0)
    {
      create_Node_Helper(NULL, baseUri, new xqpStringStore("json"), &element);
      create_Attribute_Helper(element, new xqpStringStore("type"), new xqpStringStore("object"));

      for ( vectIter=vect->begin(); vectIter != vect->end(); ++vectIter )
      {
        store::Item_t new_node = NULL;
        parse_Json_value(&*vectIter, element, baseUri, &new_node);
      }
    }

    return true;
  }

  bool JSON_serialize(const store::Item* element, xqpStringStore_t& json_string, xqpStringStore_t& error_log)
  {
    json_string = new xqpStringStore("");
    bool result = true;

    if (element == NULL)
    {
      error_log = new xqpStringStore("Passed a NULL element to the JSON serializer.");
      return false;
    }

    xqpStringStore_t name = element->getNodeName()->getStringValue();

    if( name->byteCompare("json") != 0 )
    {
      error_log = new xqpStringStore("This is not a JSON element.");
      return false;
    }

    json_string->append_in_place('{');

    result = parse_child(element, json_string, error_log);

    if(result)
      json_string->append_in_place('}');

    return result;
  }


  //helper functions
  bool create_Type_Helper(store::Item_t& result, xqpStringStore_t type_name)
  {
    xqpStringStore_t xs_ns = new xqpStringStore("http://www.w3.org/2001/XMLSchema");
    xqpStringStore_t xs_pre = new xqpStringStore("xs");
    return GENV_ITEMFACTORY->createQName(result, xs_ns, xs_pre, type_name);
  }

  bool create_QName_Helper(store::Item_t& result, xqpStringStore_t name)
  {
    xqpStringStore_t ns = new xqpStringStore("");//ZORBA_JSON_FN_NS);
    xqpStringStore_t pre = new xqpStringStore("");//zorba-json");
    return GENV_ITEMFACTORY->createQName(result, ns, pre, name);
  }

  bool create_Node_Helper(store::Item_t parent, xqpStringStore_t baseUri, xqpStringStore_t name, store::Item_t* result)
  {
    store::Item_t qname, temp_result, type_qname;
    store::NsBindings bindings;

    create_QName_Helper(qname, name);
    create_Type_Helper(type_qname, new xqpStringStore("untyped"));

    bool status = GENV_ITEMFACTORY->createElementNode(temp_result,
                                                      parent,
                                                      -1,
                                                      qname,
                                                      type_qname,
                                                      true,
                                                      false,
                                                      false,
                                                      false,
                                                      bindings,
                                                      baseUri,
                                                      true);

    if (result != NULL)
      *result = temp_result;

    return status;
  }

  bool create_Attribute_Helper(store::Item_t parent, xqpStringStore_t name, xqpStringStore_t value, store::Item_t* result)
  {
    store::Item_t qname, temp_result, str_item;
    store::Item_t type_qname;

    create_QName_Helper(qname, name);
    create_Type_Helper(type_qname, new xqpStringStore("string"));

    GENV_ITEMFACTORY->createString(str_item, value);
    GENV_ITEMFACTORY->createAttributeNode(temp_result,
                                          parent,<commit_msg>node copies during updates are done before pul apply + removed DAG nodes<commit_after>/*
 * Copyright 2006-2008 The FLWOR Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "util/converters/json_converter.h"

#include "json/parser.h"
#include "json/value.h"

#include "system/globalenv.h"
#include "context/namespace_context.h"

namespace zorba {
  json::value* getValue(const char* aJSON, const unsigned int aLen);

  bool create_Node_Helper(store::Item_t parent,
                          xqpStringStore_t baseUri,
                          xqpStringStore_t name,
                          store::Item_t* result = NULL);

  bool create_Attribute_Helper( store::Item_t parent,
                                xqpStringStore_t name,
                                xqpStringStore_t value,
                                store::Item_t* result = NULL);

  bool create_Pair_Helper(store::Item_t parent,
                          xqpStringStore_t baseUri,
                          store::Item_t* result,
                          xqpStringStore_t name,
                          xqpStringStore_t type,
                          xqpStringStore_t value = NULL);

  void parse_Json_value(json::value** value,
                        store::Item_t parent,
                        xqpStringStore_t baseUri,
                        store::Item_t* result);

  void parse_Json_ML_value( json::value** value,
                            store::Item_t parent,
                            xqpStringStore_t baseUri,
                            store::Item_t* result);

  bool parse_element( const store::Item* element,
                      xqpStringStore_t& json_string,
                      xqpStringStore_t& error_log);

  bool parse_child( const store::Item* element,
                    xqpStringStore_t& json_string,
                    xqpStringStore_t& error_log);

  bool get_value( const store::Item* element,
                  xqpStringStore_t& value);

  bool JSON_parse(const char* aJSON_string, const unsigned int aLength,
                  store::Item_t& element, xqpStringStore_t baseUri,
                  xqp_string& error_log)
  {
    std::auto_ptr<json::value> lValue(getValue(aJSON_string, aLength));

    json::vector_list_t::iterator vectIter;
    json::vector_list_t *vect = lValue->getchildrenlist();
    if(vect != 0)
    {
      create_Node_Helper(NULL, baseUri, new xqpStringStore("json"), &element);
      create_Attribute_Helper(element, new xqpStringStore("type"), new xqpStringStore("object"));

      for ( vectIter=vect->begin(); vectIter != vect->end(); ++vectIter )
      {
        store::Item_t new_node = NULL;
        parse_Json_value(&*vectIter, element, baseUri, &new_node);
      }
    }

    return true;
  }

  bool JSON_serialize(const store::Item* element, xqpStringStore_t& json_string, xqpStringStore_t& error_log)
  {
    json_string = new xqpStringStore("");
    bool result = true;

    if (element == NULL)
    {
      error_log = new xqpStringStore("Passed a NULL element to the JSON serializer.");
      return false;
    }

    xqpStringStore_t name = element->getNodeName()->getStringValue();

    if( name->byteCompare("json") != 0 )
    {
      error_log = new xqpStringStore("This is not a JSON element.");
      return false;
    }

    json_string->append_in_place('{');

    result = parse_child(element, json_string, error_log);

    if(result)
      json_string->append_in_place('}');

    return result;
  }


  //helper functions
  bool create_Type_Helper(store::Item_t& result, xqpStringStore_t type_name)
  {
    xqpStringStore_t xs_ns = new xqpStringStore("http://www.w3.org/2001/XMLSchema");
    xqpStringStore_t xs_pre = new xqpStringStore("xs");
    return GENV_ITEMFACTORY->createQName(result, xs_ns, xs_pre, type_name);
  }

  bool create_QName_Helper(store::Item_t& result, xqpStringStore_t name)
  {
    xqpStringStore_t ns = new xqpStringStore("");//ZORBA_JSON_FN_NS);
    xqpStringStore_t pre = new xqpStringStore("");//zorba-json");
    return GENV_ITEMFACTORY->createQName(result, ns, pre, name);
  }

  bool create_Node_Helper(store::Item_t parent, xqpStringStore_t baseUri, xqpStringStore_t name, store::Item_t* result)
  {
    store::Item_t qname, temp_result, type_qname;
    store::NsBindings bindings;

    create_QName_Helper(qname, name);
    create_Type_Helper(type_qname, new xqpStringStore("untyped"));

    bool status = GENV_ITEMFACTORY->createElementNode(temp_result,
                                                      parent,
                                                      -1,
                                                      qname,
                                                      type_qname,
                                                      true,
                                                      false,
                                                      false,
                                                      false,
                                                      bindings,
                                                      baseUri);

    if (result != NULL)
      *result = temp_result;

    return status;
  }

  bool create_Attribute_Helper(store::Item_t parent, xqpStringStore_t name, xqpStringStore_t value, store::Item_t* result)
  {
    store::Item_t qname, temp_result, str_item;
    store::Item_t type_qname;

    create_QName_Helper(qname, name);
    create_Type_Helper(type_qname, new xqpStringStore("string"));

    GENV_ITEMFACTORY->createString(str_item, value);
    GENV_ITEMFACTORY->createAttributeNode(temp_result,
                                          parent,
{'fraction_non_alphanumeric': 0.08818026519157245, 'fraction_numerical': 0.0032538843244122673, 'mean_word_length': 1.9854298203011171, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 46, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '116173', 'n_tokens_mistral': 3470, 'n_tokens_neox': 3344, 'n_words': 785}