go_convert

package module
v1.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 18, 2022 License: MIT Imports: 8 Imported by: 1

README

Go package that converts

Install

go get -u github.com/advancemg/go-convert

Importing

import github.com/advancemg/go-convert

Usage

Code example

package main

import (
	"fmt"
	converter "github.com/advancemg/go-convert"
)

func main() {
	xml := []byte(`<?xml version="1.0" encoding="UTF-8"?><hello>world</hello>`)
	json, err := converter.XmlToJson(xml)
	if err != nil {
		panic("Error converter.XmlToJson...")
	}
	fmt.Println(string(json))
	// {"hello":"world"}

	toXml, err := converter.JsonToXml(json)
	if err != nil {
		panic("Error converter.JsonToXml...")
	}
	fmt.Println(string(toXml))
	//<hello>world</hello>
}

Input


<responseProgramBreaksV2Light xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <BreakList>
        <b BlockID="117952183" HasAucSpots="false" VM="180" VR="0">
            <Booked RankID="2" VM="175" VR="0"/>
        </b>
        <b BlockID="117952235" HasAucSpots="false" VM="180" VR="0"/>
        <b BlockID="117952251" HasAucSpots="false" VM="180" VR="0">
            <Booked RankID="2" VM="125" VR="0"/>
            <Booked RankID="3" VM="10" VR="0"/>
        </b>
    </BreakList>
</responseProgramBreaksV2Light>

Output

{
  "responseProgramBreaksV2Light": {
    "BreakList": [
      {
        "b": {
          "Booked": {
            "attributes": {
              "RankID": "2",
              "VM": "175",
              "VR": "0"
            }
          },
          "attributes": {
            "BlockID": "117952183",
            "HasAucSpots": "false",
            "VM": "180",
            "VR": "0"
          }
        }
      },
      {
        "b": {
          "attributes": {
            "BlockID": "117952235",
            "HasAucSpots": "false",
            "VM": "180",
            "VR": "0"
          }
        }
      },
      {
        "b": {
          "Booked": [
            {
              "attributes": {
                "RankID": "2",
                "VM": "125",
                "VR": "0"
              }
            },
            {
              "attributes": {
                "RankID": "3",
                "VM": "10",
                "VR": "0"
              }
            }
          ],
          "attributes": {
            "BlockID": "117952251",
            "HasAucSpots": "false",
            "VM": "180",
            "VR": "0"
          }
        }
      }
    ],
    "attributes": {
      "xmlns:xsi": "\"http://www.w3.org/2001/XMLSchema-instance\""
    }
  }
}

Input

{
  "GetAdvMessages": {
    "Advertisers": {
      "ID": "1"
    },
    "AdvertisingMessageIDs": [
      {
        "ID": "1"
      },
      {
        "ID": "2"
      }
    ],
    "Aspects": {
      "ID": "2"
    },
    "CreationDateEnd": "2019-03-02",
    "CreationDateStart": "2019-03-01",
    "FillMaterialTags": "true",
    "attributes": {
      "xmlns:xsi": "\"http://www.w3.org/2001/XMLSchema-instance\""
    }
  }
}

Output


<GetAdvMessages xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Advertisers>
        <ID>1</ID>
    </Advertisers>
    <AdvertisingMessageIDs>
        <ID>1</ID>
        <ID>2</ID>
    </AdvertisingMessageIDs>
    <Aspects>
        <ID>2</ID>
    </Aspects>
    <CreationDateEnd>2019-03-02</CreationDateEnd>
    <CreationDateStart>2019-03-01</CreationDateStart>
    <FillMaterialTags>true</FillMaterialTags>
</GetAdvMessages>

Benchmark

go test -bench=. -benchmem

XmlToJson

Op ns/op B/op allocs/op
17607 67120 41216 581

JsonToXml

Op ns/op B/op allocs/op
43179 33029 15231 372

Test

go test -v

Contributing

Feel free to contribute to this project if you want to fix/extend/improve it.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FileXmlToJson

func FileXmlToJson(file string) ([]byte, error)

FileXmlToJson covert xml file to json. Output []byte and error.

func JsonToXml

func JsonToXml(input []byte) ([]byte, error)

JsonToXml covert json to xml. Output []byte and error.

func XmlToJson

func XmlToJson(input []byte) ([]byte, error)

XmlToJson covert xmt to json. Output []byte and error.

func ZipXmlToJson added in v1.0.7

func ZipXmlToJson(input []byte) ([]byte, error)

ZipXmlToJson covert xmt to json. Input gzip []byte. Output []byte and error.

Types

type SortedPair added in v1.0.4

type SortedPair struct {
	Pairs    []*UnsortedPair
	LessFunc func(a *UnsortedPair, j *UnsortedPair) bool
}

func (SortedPair) Len added in v1.0.4

func (a SortedPair) Len() int

Len SortedPair.

func (SortedPair) Less added in v1.0.4

func (a SortedPair) Less(i, j int) bool

Less SortedPair.

func (SortedPair) Swap added in v1.0.4

func (a SortedPair) Swap(i, j int)

Swap SortedPair.

type UnsortedMap added in v1.0.4

type UnsortedMap struct {
	// contains filtered or unexported fields
}

func New added in v1.0.4

func New() *UnsortedMap

New UnsortedMap.

func (*UnsortedMap) Get added in v1.0.4

func (uMap *UnsortedMap) Get(key string) (val interface{}, exists bool)

Get value UnsortedMap.

func (*UnsortedMap) Keys added in v1.0.4

func (uMap *UnsortedMap) Keys() []string

Keys UnsortedMap.

func (UnsortedMap) MarshalJSON added in v1.0.4

func (uMap UnsortedMap) MarshalJSON() ([]byte, error)

MarshalJSON UnsortedMap.

func (*UnsortedMap) Remove added in v1.0.4

func (uMap *UnsortedMap) Remove(key string)

Remove value UnsortedMap.

func (*UnsortedMap) Set added in v1.0.4

func (uMap *UnsortedMap) Set(key string, value interface{})

Set value UnsortedMap.

func (*UnsortedMap) SortKeys added in v1.0.4

func (uMap *UnsortedMap) SortKeys(sortFunc func(keys []string))

SortKeys map keys using your sort func

func (*UnsortedMap) SortPairs added in v1.0.4

func (uMap *UnsortedMap) SortPairs(lessFunc func(a *UnsortedPair, b *UnsortedPair) bool)

SortPairs using your sort func

func (*UnsortedMap) ToXml added in v1.0.4

func (uMap *UnsortedMap) ToXml() ([]byte, error)

ToXml convert UnsortedMap to xml.

func (*UnsortedMap) UnmarshalJSON added in v1.0.4

func (uMap *UnsortedMap) UnmarshalJSON(b []byte) error

UnmarshalJSON UnsortedMap.

func (*UnsortedMap) XmlAttributes added in v1.0.4

func (uMap *UnsortedMap) XmlAttributes() []byte

XmlAttributes UnsortedMap.

type UnsortedPair added in v1.0.4

type UnsortedPair struct {
	// contains filtered or unexported fields
}

func (*UnsortedPair) Key added in v1.0.4

func (kv *UnsortedPair) Key() string

Key UnsortedPair.

func (*UnsortedPair) Value added in v1.0.4

func (kv *UnsortedPair) Value() interface{}

Value UnsortedPair.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL