urn8141.go raw

   1  package urn
   2  
   3  import (
   4  	"encoding/json"
   5  	"fmt"
   6  )
   7  
   8  const errInvalidURN8141 = "invalid URN per RFC 8141: %s"
   9  
  10  type URN8141 struct {
  11  	*URN
  12  }
  13  
  14  func (u URN8141) MarshalJSON() ([]byte, error) {
  15  	return json.Marshal(u.String())
  16  }
  17  
  18  func (u *URN8141) UnmarshalJSON(bytes []byte) error {
  19  	var str string
  20  	if err := json.Unmarshal(bytes, &str); err != nil {
  21  		return err
  22  	}
  23  	if value, ok := Parse([]byte(str), WithParsingMode(RFC8141Only)); !ok {
  24  		return fmt.Errorf(errInvalidURN8141, str)
  25  	} else {
  26  		*u = URN8141{value}
  27  	}
  28  
  29  	return nil
  30  }
  31